Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Question regarding using macros and the API for automation 2

Status
Not open for further replies.

jistre

Mechanical
Oct 1, 2003
1,147
Okay, I have here what seems to be a simple task, but for the life of me, I can't make it happen, and I can't find what I'm looking for in documentation. I'm guessing I'm just too dense to find the information and make sense of it; I'm a mechanical engineer, not a software engineer.

What I'm looking to do is create a macro that will make a mate that makes two planes coincident. I have found many macros out there that will do exactly that given that you select two planes and fire the macro, and I use one extensively when modelling assemblies. It's a HUGE productivity booster. What I'm looking for is a bit more specialized.

I don't know if you guys have noticed, but the fasteners in the toolbox use a pretty standard scheme for naming their planes. Plane 1 and plane 2 are parallel to the "axis" of the fastener, and plane 3 is the perpendicular. When parts are automatically placed from the toolbox, 5 degrees of freedom are typically removed, leaving rotation around the "axis" free. If you take two fasteners and mate their plane 1's coincident, you lock up that last degree and make the model fully defined. I'm looking for a macro that will do this. I want to select the two fasteners that I want mated, fire the macro, and have it take the two planes named "plane 1" in each, and then add a coincident mate and close the mate property manager.

I know there's got to be a way to do it, but I'm not that fluent with the VBA interface for this program. I suspect one of you gurus out there can help me and point me in the right direction.

If you can also tell me how to make a macro collapse the feature manager tree, that'd be great too, and I'd name my firstborn after you (or perhaps something that sounds nothing like your name.)

Oh yeah,

Thanks!
 
Replies continue below

Recommended for you

This will do it. You can select two components to add the mate, but you aren't restricted to only selecting the components. You can choose any feature, vertex, edge etc from either the feature manager or the model.

Code:
Option Explicit

Sub Main()

    Dim swApp                   As SldWorks.SldWorks
    Dim swDoc                   As SldWorks.ModelDoc2
    Dim swAssy                  As SldWorks.AssemblyDoc
    Dim SelMgr                  As SldWorks.SelectionMgr
    Dim ParentComponent         As SldWorks.Component2
    Dim swMate                  As SldWorks.Mate2
    Dim sPlaneName              As String
    Dim sPlaneOne               As String
    Dim sPlaneTwo               As String
    Dim BoolStatus              As Boolean
    Dim lErrorStatus            As Long
    
    Const TWO = 2
    
    Set swApp = Application.SldWorks
    Set swDoc = swApp.ActiveDoc
    sPlaneName = "Plane 1"
      
    If (IsEmpty(swApp) Or IsNull(swApp)) Then
            MsgBox "Could not connect to SolidWorks"
        Exit Sub
    End If
    
    'if document is not an assembly then exit
    If Not (swDoc.GetType = swDocASSEMBLY) Then
        MsgBox "An assembly document must be active to use this command!"
        Exit Sub
    End If
    
    'How many objects are selected?
    Set SelMgr = swDoc.SelectionManager
    
    'Exit routine if the user has not selected exactly 2 entities
    If Not (SelMgr.GetSelectedObjectCount = TWO) Then
        MsgBox "Please select two assembly component features"
        Exit Sub
    End If
    
    'Get the name of the parent component
    Set ParentComponent = SelMgr.GetSelectedObjectsComponent3(1, -1)
    sPlaneOne = sPlaneName & "@" & ParentComponent.Name & "@" & swDoc.GetTitle
    Set ParentComponent = SelMgr.GetSelectedObjectsComponent3(2, -1)
    sPlaneTwo = sPlaneName & "@" & ParentComponent.Name & "@" & swDoc.GetTitle
    
    'Clear Selections
    swDoc.ClearSelection2 (True)
    swDoc.EditRebuild3
   
   'Select the Front planes of the two components
    BoolStatus = swDoc.Extension.SelectByID2(sPlaneOne, "PLANE", 0, 0, 0, True, 0, Nothing, 0)
    BoolStatus = swDoc.Extension.SelectByID2(sPlaneTwo, "PLANE", 0, 0, 0, True, 0, Nothing, 0)
     
    'Add the coincident mate
    Set swAssy = swApp.ActiveDoc
    Set swMate = swAssy.AddMate3(swMateCOINCIDENT, swMateAlignCLOSEST, False, 0, 0, 0, 0, 0, 0, 0, 0, False, lErrorStatus)
    If (swMate Is Nothing) Then MsgBox "Error adding coincident mate"
     
    swDoc.EditRebuild3
    
End Sub

In the API help there is an example which demonstrates changing the feature manager width. It is "Change Width of FeatureManager Design Tree Example (VB)"


 
That's exactly what I needed, Stoker. Thank you. As for collapsing the feature manager tree, I found how to change the width of the feature manager itself, but I've yet to find how to actually collapse each expanded subassembly and part in an assembly to just see first level components.

Thanks again.
 
One thing doesn't work in the macro, though. I finally tracked it down and got it to work, so I'll share in case anyone else wants to use this macro.

Code:
Option Explicit

Sub Main()

    Dim swApp                   As SldWorks.SldWorks
    Dim swDoc                   As SldWorks.ModelDoc2
    Dim swAssy                  As SldWorks.AssemblyDoc
    Dim SelMgr                  As SldWorks.SelectionMgr
    Dim ParentComponent         As SldWorks.Component2
    Dim swMate                  As SldWorks.Mate2
    Dim sPlaneName              As String
    Dim sPlaneOne               As String
    Dim sPlaneOneA              As String
    Dim sPlaneTwoA              As String
    Dim sPlaneTwo               As String
    Dim BoolStatus              As Boolean
    Dim lErrorStatus            As Long
    
    Const TWO = 2
    
    Set swApp = Application.SldWorks
    Set swDoc = swApp.ActiveDoc
    sPlaneName = "Plane1"
      
    If (IsEmpty(swApp) Or IsNull(swApp)) Then
            MsgBox "Could not connect to SolidWorks"
        Exit Sub
    End If
    
    'if document is not an assembly then exit
    If Not (swDoc.GetType = swDocASSEMBLY) Then
        MsgBox "An assembly document must be active to use this command!"
        Exit Sub
    End If
    
    'How many objects are selected?
    Set SelMgr = swDoc.SelectionManager
    
    'Exit routine if the user has not selected exactly 2 entities
    If Not (SelMgr.GetSelectedObjectCount = TWO) Then
        MsgBox "Please select two assembly component features"
        Exit Sub
    End If
    
    'Get the name of the parent component
    Set ParentComponent = SelMgr.GetSelectedObjectsComponent3(1, -1)
    sPlaneOne = Replace(sPlaneName & "@" & ParentComponent.Name & "@" & swDoc.GetTitle, ".SLDASM", "")
    Set ParentComponent = SelMgr.GetSelectedObjectsComponent3(2, -1)
    sPlaneTwo = Replace(sPlaneName & "@" & ParentComponent.Name & "@" & swDoc.GetTitle, ".SLDASM", "")
    
    'Clear Selections
    swDoc.ClearSelection2 (True)
    swDoc.EditRebuild3
   
   'Select the Front planes of the two components
    BoolStatus = swDoc.Extension.SelectByID2(sPlaneOne, "PLANE", 0, 0, 0, True, 0, Nothing, 0)
    BoolStatus = swDoc.Extension.SelectByID2(sPlaneTwo, "PLANE", 0, 0, 0, True, 0, Nothing, 0)
     
    'Add the coincident mate
    Set swAssy = swApp.ActiveDoc
    Set swMate = swAssy.AddMate3(swMateCOINCIDENT, swMateAlignCLOSEST, False, 0, 0, 0, 0, 0, 0, 0, 0, False, lErrorStatus)
    If (swMate Is Nothing) Then MsgBox "Error adding coincident mate"
     
    swDoc.EditRebuild3
    
End Sub

The lines containing "replace" were the ones giving me troble. The selected plane listings had ".SLDASM" from the filename at the end, and the mate command didn't like this. I just stripped out this text and it works fine.

Thanks to Stoker for all his help.
 
On my system I have the Windows options "Hide file extensions for known file types" checked. I suspect that you do not and that is why you see the file name also returning the extension. On most of my routines that have to deal with file names I check to see if the string also includes the extension and then modify the string if required.
 
If the macro still isn't working 100% of the time on different Users computes, I'd say it's because of the "swDoc.GetTitle" call. What it does it get the text that is displayed in the title bar. That text is dependent upon a Windows Setting at:
-My Computer/Windows Explorer (whatever it's called)
-Goto Tools/Folder Options/View Tab
-There you will find a checkbox for "Display the full path in the title bar."

Since this is a User specific setting, I recommend that you never use "GetTitle". Instead use "GetPathName", that way you will get consistent results.

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor