×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

API Question

API Question

API Question

(OP)
I'm attempting to make a macro (not something that I'm very good at) that given a width and height of a plate, will create a centered array of holes at 2" on center.  I can get it to create the right number of holes, but I can't get them to center, so that I often end up .75" from one side and 1.25" from the other side.  

The code Part.Parameter("DimA@Sketch1").SystemValue = (Insert formula here) doesn't seem to be taking.  If I set it equal to a simple numerical value, however, it changes.  The actual formula I used is
Width / 2 + 1 / 39.3700787 - ((39.3700787 * Width + 1.25) \ 2)
where Width is defined in inches, and I know that it is defined, since the formula to determine the number of holes uses it (that's the second half of the above equation).  I'm at a bit of a loss here.

RE: API Question

SilasH,

SystemValue is in meters.  Is this what the 39.3700787 is doing.  I normally use 25.4 to do all my conversions.

Regards,

Regg

RE: API Question

(OP)
Yes, that's the conversion into inches.  My problem is that for some reason, those lines aren't running when I have the formula in there, but they will if I have a simple numeric value.  I used the Width value a couple lines previous to it, and it worked just fine.  Here's the complete code, in all of its hack and slash glory:

Dim WidthHoles As Double
Dim HeightHoles As Double
Dim XOffset As Double
Dim YOffset As Double


Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc
Set SelMgr = Part.SelectionManager

GPForm.Show
Height = GPForm.Height / 39.3700787
Width = GPForm.Width / 39.3700787
Mil = GPForm.Mil / 39.3700787
XOffset = Width / 2 + 1 / 39.3700787 - ((39.3700787 * Width + 1.25) \ 2)
YOffset = Height / 2 + 1 / 39.3700787 - ((39.3700787 * Height + 1.25) \ 2)

Set GPForm = Nothing
Part.AddConfiguration "GP" & Height * 39.3700787 & "-" & Mil * 39370.0787 & "-" & Width * 39.3700787, "", "", 0, 0, 0, 1, 256

Part.Parameter("width@Sketch1").SystemValue = Width

Part.Parameter("height@Sketch1").SystemValue = Height

Part.Parameter("Thickness@Sheet-Metal1").SystemValue = Mil
Part.Parameter("D1@LPattern1").SystemValue = (39.3700787 * Width + 1.25) \ 2
Part.Parameter("D2@LPattern1").SystemValue = (39.3700787 * Height + 1.25) \ 2
Part.ForceRebuild
Part.Parameter("y-offset@Sketch3").SystemValue = 0.0254
Part.Parameter("x-offset@Sketch3").SystemValue = 0.0254
Part.Parameter("x-offset@Sketch3").SystemValue = Width / 2 + 1 / 39.3700787 - ((39.3700787 * Width + 1.25) \ 2)
Part.Parameter("y-offset@Sketch3").SystemValue = Height / 2 + 1 / 39.3700787 - ((39.3700787 * Height + 1.25) \ 2)
'Part.Parameter("x-offset@Sketch3").SystemValue = XOffset
'Part.Parameter("y-offset@Sketch3").SystemValue = YOffset
Part.ForceRebuild

End Sub



You can see I've tried a couple different ways to get the holes to center, by direct formula, and by variable.  Still, no luck.

RE: API Question

Here's one problem - your final "divide by 2" operation is attempting to use a backslash (\) rather than a frontslash (/).

RE: API Question

The values still have to be in meters when setting the system value

RE: API Question

(OP)
From what I read in the API notes, the \ returns an integer value, which is what I wanted, i.e. 100 \ 3 = 33.  And that formula works correctly to give me the number of holes on the sheet.

RE: API Question

SilasH,

From your original post:

Quote:

Width / 2 + 1 / 39.3700787 - ((39.3700787 * Width + 1.25) \ 2)
where Width is defined in inches, and I know that it is defined, since the formula to determine the number of holes uses it (that's the second half of the above equation).  

The first half of that formula (Width / 2 + 1 / 39.3700787) is converting an inch value to meters, so if the Width was 6 inches entered into the Dialog Box, that value equates to 0.1524 here...OK that looks all right, not the way I would do it, but it should work.

The second half of that formula ((39.3700787 * Width + 1.25) \ 2) is dealing with whole numbers like 1, 2,3, etc...Not in sync with the first half of the formula.

Resulting in a decimal minus a whole number, which is always going to be negative...and cause problems with the linear pattern.

So the error is in your formula, no matter where you put it.

I did fix the code, but considering that the "Part.AddConfiguration" line is wrong. And that you use "/39.3700787" instead of "*0.0254"...I really don't want to do all your homework for you.

Ken

RE: API Question

To build on Ken's response, you need to figure out whether the result of your

Width / 2 + 1 / 39.3700787 - ((39.3700787 * Width + 1.25) \ 2)

formula is giving the value you expect.  Try

MsgBox Width / 2 + 1 / 39.3700787 - ((39.3700787 * Width + 1.25) \ 2)

RE: API Question

(OP)
Ahhhh, I gotcha.  Doh.  Thanks!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources