Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations IDS on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

API help - DimensionTolerance.SetFont

Status
Not open for further replies.

8Complex

Mechanical
Jan 28, 2004
38
Can someone please help me with this? I am going out of my mind trying to get this to work, but all of my tests and guesses have failed.

I've already got a larger macro to put this code into, so all I want is to be able to run a simple macro and change the selected dimension tolerance font scale to 0.75. I will integrate it into my other code once I get it working.

After snagging some other code I found on here and messing with it appropriately, I've had no luck. Here is what I've got it trimmed down to :

Code:
Dim swApp As SldWorks.SldWorks
Dim swDoc As SldWorks.ModelDoc2
Dim swDim As SldWorks.Dimension
Dim swTol As SldWorks.DimensionTolerance

Sub main()

Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
Set swTol = swDim.Tolerance

swTol.SetFont(False, True, "0.75")

End Sub

This keeps throwing a "Compile Error / Expected: =". When I try to throw an = in there, it has me set the values again, which I still just can't make happy. What exactly is this looking for, and am I calling it properly?

FYI, I'm on SW2006 SP3.1.
 
Replies continue below

Recommended for you

Two immediate problems:

1. You are not actually selecting a dimension object for "swDim" to perform this action on.

2. Your syntax is wrong on the "swTol.SetFont" command. You are using function syntax while calling the API as a sub. Get rid of the parentheses.

[bat]I could be the world's greatest underachiever, if I could just learn to apply myself.[bat]
-SolidWorks API VB programming help
 
Is what you've posted the entire macro you're trying to run? If so, you've not told SW which dimension you want it to go to work on. You have to get a handle on the specific dimension you're interested in. Assuming you want to operate on the selected dimension, it'll be something like this, which will do what you want to do to every selected dimension that has a tolerance shown:

Code:
Dim swApp As SldWorks.SldWorks
Dim swDoc As SldWorks.ModelDoc2
Dim SelMgr As SldWorks.SelectionMgr
Dim swDispDim As SldWorks.DisplayDimension
Dim swDim As SldWorks.Dimension
Dim swDimTol As SldWorks.DimensionTolerance
Dim boolstatus As Boolean
Dim i As Long

Sub main()

Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
Set SelMgr = swDoc.SelectionManager

For i = 1 To SelMgr.GetSelectedObjectCount2(-1)
    If SelMgr.GetSelectedObjectType3(i, -1) = swSelDIMENSIONS Then
        Set swDispDim = SelMgr.GetSelectedObject6(i, -1)
        Set swDim = swDispDim.GetDimension
        Set swDimTol = swDim.Tolerance
        If swDimTol.Type <> swTolNONE Then
            swDimTol.SetFont False, True, 0.75
        End If
    End If
Next i
swDoc.GraphicsRedraw2
End Sub

Note that you had "0.75" instead of just 0.75. SetFont expects a double, not a string.
 
Thanks for the answers, guys.


I think I understand now, I was misunderstanding how the targetting of an object works. I was thinking that if it was selected, it was automatically the object that the macro works on. I see now in the macro above that you actually have to set an object variable to the selected dimension, then perform the SetFont command upon it.

One question, though... if you're defining the objects in the code (set swDispDim, set swDim, set swDimTol) then why bother setting them in the overall variables prior to executing? Also, what does the boolstatus line do? It seems unnecessary to me, and the macro appears to work without it.
 
The variables you define in the code are only pointers or containers for the actual SW objects you wish to edit. When they are first defined, they are empty. It is not until you assign an actual object (i.e. using "Set") that the variable has something to edit. Then the variables become the gateway through which the VB code can interact with the objects.

You are correct about "boolstatus". In many cases it can be ignored, provided you use "Sub" syntax and not "Function" syntax. In this case. "boolstatus" is just a return or "flag" variable to record whether a function performed successfully.

[bat]Honesty may be the best policy, but insanity is a better defense.[bat]
-SolidWorks API VB programming help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor