×
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 for Flipping Mate Alignment

API for Flipping Mate Alignment

API for Flipping Mate Alignment

(OP)
Can someone help me create a macro/program to flip the mate alignment?  The following are the main lines of the code that I have started to use from a recorded macro.  The first line seems to work every time for selecting the mate if it is preselected.  The next line for picking the mate entity is the problem.  I can not figure out how to make it pick the correct surface.  When I get it to pick the correct entities, the command swModel.EditMate2 works.  Or is there a better approach?  I am using SW2004. Thank you.

boolstatus = swModel.Extension.SelectByID(swFeat.Name, "MATE", 0, 0, 0, False, 0, Nothing)

boolstatus = swModel.Extension.SelectByID("", swMate.MateEntity(1).ReferenceType, swMate.MateEntity(1).EntityParams(0), swMate.MateEntity(1).EntityParams(1), swMate.MateEntity(1).EntityParams(2), True, 1, Nothing)

'Change alignment indicator
    If swMate.alignment = 1 Then
        NewAlign = 0
    Else
        NewAlign = 1
    End If
    
swModel.EditMate2 swMate.Type, NewAlign, swMate.CanBeFlipped, 0.01, 0, 0, 1, 1, 0.5235987755983, 0.5235987755983, 0.5235987755983, longstatus

RE: API for Flipping Mate Alignment

Unfortunately, I only have SW2001+ at the moment, so my advice may be out of step.  However, some things to consider:

•When a macro records selections, it does not do so in a manner that is easily adapted to a generic case.  The macro recorder is basically just recording your mouse click location and the subsequent action.

•Learn how the Selection Manager works.  This is how you can access objects that are preselected when a macro starts.

•When digging through the Selection Manager's selections, it is sometimes necessary to do a little more manipulation to get the object you really want.  For instance, you may need to get a component or body when you have a face or edge selected.  In your case, you might want the mate entities once you have the mate.  Try Mate::GetMateEntities.

•You can make selections during macro run time if you use forms.  Set the form ShowModal property to False and you can interact with SolidWorks while the macro is running.

More to come when I get a chance to look at more recent version of SW.

"Great ideas need landing gear as well as wings."--C. D. Jackson
http://www.EsoxRepublic.com

RE: API for Flipping Mate Alignment

(OP)
I will try your suggestions.  I thought I tried the Selection Manager and it only sees the selected mate and not the entities but I need to learn more about how it works.  I will not get a chance to try everything until Friday.

Thank you,

RE: API for Flipping Mate Alignment

The Selection Manager forms an array of all the selected objects, in the order they were selected.  But unlike the standard array, I think the first object selected is actually the "1" member of the array (i.e. it doesn't start with zero, it starts with one).

Look up SelectionMgr in the SW API Help, you should find some examples there as well.

Ken

RE: API for Flipping Mate Alignment

re:"I thought I tried the Selection Manager and it only sees the selected mate and not the entities..."

You can get the entities from the mate using Mate::GetMateEntities.

"Great ideas need landing gear as well as wings."--C. D. Jackson
http://www.EsoxRepublic.com

RE: API for Flipping Mate Alignment

(OP)
It looks like in 2004, Mate::GetMateEntities has been replaced with Mate2.MateEntity.  I can get the mate entity but how do I select it?  Selection Manager works fine for the mate itself because it is preselected.  I am still looking through the API help but I do not think I will find an example that will point me in the right direction.

RE: API for Flipping Mate Alignment

You don't need to select the entities.  What you need to do is assign the mate entities to object variables so that you can manipulate them with VB.

RE: API for Flipping Mate Alignment

(OP)
EditMate2 assumes that the mate and the two entities are selected otherwise it fails.  EditMate2 does not have any location to input objects, just the variables of the mate itself.

RE: API for Flipping Mate Alignment

I stand corrected.

Could you access the mate entities with Mate2.MateEntity, get the info you need from them, and then select them with the selection manager?

RE: API for Flipping Mate Alignment

(OP)
I sent an email to SW API Support on this problem and below is the code that was sent back.  It appears to work every time but I have a lot to learn on how to find some of the commands that were used.


'------------------------------------------------
Option Explicit

Public Enum swMateType_e
    swMateCOINCIDENT = 0
    swMateCONCENTRIC = 1
    swMatePERPENDICULAR = 2
    swMatePARALLEL = 3
    swMateTANGENT = 4
    swMateDISTANCE = 5
    swMateANGLE = 6
    swMateUNKNOWN = 7
    swMateSYMMETRIC = 8
    swMateCAMFOLLOWER = 9
    swMateGEAR = 10
End Enum

Public Enum swMateAlign_e
    swMateAlignALIGNED = 0
    swMateAlignANTI_ALIGNED = 1
    swMateAlignCLOSEST = 2
End Enum

Public Enum swMateEntity2ReferenceType_e
    swMateEntity2ReferenceType_Point = 0
    swMateEntity2ReferenceType_Line = 1
    swMateEntity2ReferenceType_Circle = 2
    swMateEntity2ReferenceType_Plane = 3
    swMateEntity2ReferenceType_Cylinder = 4
    swMateEntity2ReferenceType_Sphere = 5
    swMateEntity2ReferenceType_Set = 6
    swMateEntity2ReferenceType_Cone = 7
    swMateEntity2ReferenceType_SweptSurface = 8
    swMateEntity2ReferenceType_MultipleSurface = 9
    swMateEntity2ReferenceType_GenSurface = 10
    swMateEntity2ReferenceType_Ellipse = 11
    swMateEntity2ReferenceType_GeneralCurve = 12
    swMateEntity2ReferenceType_UNKNOWN = 13
End Enum

Public Enum swAddMateError_e
    swAddMateError_ErrorUknown = 0
    swAddMateError_NoError = 1
    swAddMateError_IncorrectMateType = 2
    swAddMateError_IncorrectAlignment = 3
    swAddMateError_IncorrectSelections = 4
    swAddMateError_OverDefinedAssembly = 5
End Enum

Function SelectMateEntity _
( _
    swApp As SldWorks.SldWorks, _
    swModel As SldWorks.ModelDoc2, _
    swMateEnt As SldWorks.MateEntity2, _
    nMark As Long _
) As Boolean
    Dim swEnt                   As SldWorks.entity
    Dim swSelMgr                As SldWorks.SelectionMgr
    Dim swSelData               As SldWorks.SelectData
    Dim bRet                    As Boolean

    Select Case swMateEnt.ReferenceType
        Case swMateEntity2ReferenceType_Point, _
                swMateEntity2ReferenceType_Line, _
                swMateEntity2ReferenceType_Circle, _
                swMateEntity2ReferenceType_Plane, _
                swMateEntity2ReferenceType_Cylinder, _
                swMateEntity2ReferenceType_Sphere, _
                swMateEntity2ReferenceType_Cone, _
                swMateEntity2ReferenceType_SweptSurface
            Set swSelMgr = swModel.SelectionManager
            Set swSelData = swSelMgr.CreateSelectData
            Set swEnt = swMateEnt.reference: Debug.Assert Not swEnt Is Nothing
            
            swSelData.mark = nMark
            
            bRet = swEnt.Select4(True, swSelData): Debug.Assert bRet
            SelectMateEntity = bRet
            Exit Function
        
        Case swMateEntity2ReferenceType_Set, _
                swMateEntity2ReferenceType_MultipleSurface, _
                swMateEntity2ReferenceType_GenSurface, _
                swMateEntity2ReferenceType_Ellipse, _
                swMateEntity2ReferenceType_GeneralCurve, _
                swMateEntity2ReferenceType_UNKNOWN
            Debug.Assert False
        
        Case Else
            Debug.Assert False
    End Select
    
    SelectMateEntity = False
End Function

Sub main()
    Dim swApp                   As SldWorks.SldWorks
    Dim swModel                 As SldWorks.ModelDoc2
    Dim swAssy                  As SldWorks.AssemblyDoc
    Dim swSelMgr                As SldWorks.SelectionMgr
    Dim swFeat                  As SldWorks.feature
    Dim swMate                  As SldWorks.Mate2
    Dim swDispDim               As SldWorks.DisplayDimension
    Dim swDim                   As SldWorks.Dimension
    Dim sVarType                As String
    Dim nVarFactor              As Double
    Dim nMateDist               As Double
    Dim nNumMateEnt             As Long
    Dim swMateEnt()             As SldWorks.MateEntity2
    Dim vMateEntPar             As Variant
    Dim swComp                  As SldWorks.Component2
    Dim nNewMateAlign           As Long
    Dim nRetVal                 As Long
    Dim i                       As Long
    Dim bRet                    As Boolean
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swAssy = swModel
    Set swSelMgr = swModel.SelectionManager
    Set swFeat = swSelMgr.GetSelectedObject5(1)
    Set swMate = swFeat.GetSpecificFeature2
    Set swDispDim = swMate.DisplayDimension
    
    Debug.Print "File = " & swModel.GetPathName
    Debug.Print "  " & swFeat.Name
    Debug.Print "    Type           = " & swMate.Type
    Debug.Print "    Alignment      = " & swMate.alignment
    Debug.Print "    CanBeFlipped   = " & swMate.CanBeFlipped
    
    Select Case swMate.Type
        Case swMateANGLE
            sVarType = " deg"
            nVarFactor = 57.3
            
        Case swMateDISTANCE
            sVarType = " mm"
            nVarFactor = 1000#
    
        Case swMateGEAR
            sVarType = " ratio"
            nVarFactor = 1#
    End Select
    
    If swMateANGLE = swMate.Type Or swMateDISTANCE = swMate.Type Then
        Debug.Print "    MaxVar         = " & swMate.MaximumVariation * nVarFactor & sVarType
        Debug.Print "    MinVar         = " & swMate.MinimumVariation * nVarFactor & sVarType
    End If
    
    If Not swDispDim Is Nothing Then
        Set swDim = swDispDim.GetDimension
        
        nMateDist = swDim.GetSystemValue2("")
    
        ' broken:
        '   SPR 200810 - "Mate2::DisplayDimension returns incorrect value for a swMateGEAR mate if first ratio is not unity"
        Debug.Print "    Dim Value      = " & nMateDist * nVarFactor & sVarType
    End If
    
    nNumMateEnt = swMate.GetMateEntityCount
    ReDim swMateEnt(nNumMateEnt)
    For i = 0 To nNumMateEnt - 1
        Set swMateEnt(i) = swMate.MateEntity(i)
        Set swComp = swMateEnt(i).ReferenceComponent
        
        vMateEntPar = swMateEnt(i).EntityParams
        
        Debug.Print "      RefType(" & i & ")   = " & swMateEnt(i).ReferenceType
        Debug.Print "        Component          = " & swComp.Name2 & " (" & swComp.ReferencedConfiguration & ") --> " & swComp.GetPathName
        Debug.Print "        Point              = (" & vMateEntPar(0) * 1000# & ", " & vMateEntPar(1) * 1000# & ", " & vMateEntPar(2) * 1000# & ") mm"
        Debug.Print "        Vector             = (" & vMateEntPar(3) & ", " & vMateEntPar(4) & ", " & vMateEntPar(5) & ")"
        Debug.Print "        Radius 1           = " & vMateEntPar(6) * 1000# & " mm"
        Debug.Print "        Radius 2           = " & vMateEntPar(7) * 1000# & " mm"
    Next i
    
    Select Case swMate.Type
        ' cannot change alignment on these mate types
        Case swMateGEAR
            Exit Sub
    End Select
    
    If swMateAlignALIGNED = swMate.alignment Then
        nNewMateAlign = swMateAlignANTI_ALIGNED
    Else
        If swMateAlignANTI_ALIGNED = swMate.alignment Then
            nNewMateAlign = swMateAlignALIGNED
        Else
            ' closest alignment, so changing alignment does not make sense
            Debug.Assert swMateAlignCLOSEST = swMate.alignment
            Exit Sub
        End If
    End If

    swModel.ClearSelection2 True
    
    For i = 0 To nNumMateEnt - 1
        bRet = SelectMateEntity(swApp, swModel, swMateEnt(i), 1): Debug.Assert bRet
    Next i
    
    ' AssemblyDoc::EditMate2 requires mate feature to be last selected object
    bRet = swFeat.Select2(True, 0): Debug.Assert bRet
    
    ' broken:
    '   SPR 200809 - "AssemblyDoc::EditMate2 returns swAddMateError_ErrorUknown when successful"
    swAssy.EditMate2 _
        swMate.Type, _
        nNewMateAlign, _
        True, _
        nMateDist, _
        swMate.MaximumVariation, _
        swMate.MinimumVariation, _
        0#, _
        0#, _
        nMateDist, _
        swMate.MaximumVariation, _
        swMate.MinimumVariation, nRetVal
    ' do not assert since may overdefine assy or other error
    'Debug.Assert swAddMateError_NoError = nRetVal
    
    ' do not assert since assy may have rebuild errors due to changing mate alignment
    bRet = swModel.EditRebuild3
    'Debug.Assert bRet
End Sub
'------------------------------------------------

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