Question regarding using macros and the API for automation
Question regarding using macros and the API for automation
(OP)
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!
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!






RE: Question regarding using macros and the API for automation
CODE
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)"
RE: Question regarding using macros and the API for automation
Thanks again.
RE: Question regarding using macros and the API for automation
CODE
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.
RE: Question regarding using macros and the API for automation
RE: Question regarding using macros and the API for automation
-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