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.
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
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
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
RE: API Question
RE: API Question
RE: API Question
From your original post:
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
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