2 Replies Latest reply on Aug 23, 2016 7:16 AM by Viktor Lundström

    Macro to mate named coordinatesystem

    Viktor Lundström

      Hello

       

      I have an assembly with several parts that contain named coordinate system as in code below "INF_KST_BOTTEN".

      I would like to run a Macro that regardless of part searches for predefined names of coordsys. and add coincident mates between them.

       

      Is it possible to write a macro for this task? any pointers where i should begin?

       

      I made a dirty recording to show what i want to do.

      Dim swApp As Object

       

       

      Dim Part As Object

      Dim boolstatus As Boolean

      Dim longstatus As Long, longwarnings As Long

       

       

      Sub main()

       

       

      Set swApp = _

      Application.SldWorks

       

       

      Set Part = swApp.ActiveDoc

      boolstatus = Part.Extension.SelectByID2("INF_KST_BOTTEN@0665733908-1@0665733901/51520-00 Kolvstång 63 32 A-1@0665733908", "COORDSYS", 0, 0, 0, False, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("INF_KST_BOTTEN@0665733909-1@0665733901/105-1006 Botten plan 63-1@0665733909", "COORDSYS", 0, 0, 0, True, 0, Nothing, 0)

      Dim myMate As Object

      Set myMate = Part.AddMate5(20, -1, False, 0.13042791553402, 0.001, 0.001, 0.001, 0.001, 0.5235987755983, 0.5235987755983, 0.5235987755983, False, False, 0, longstatus)

      Part.ClearSelection2 True

      Part.EditRebuild3

       

       

      End Sub

      Best Regards

       

      / Viktor

        • Re: Macro to mate named coordinatesystem
          Akshay Abitkar

          Solidworks UI does not allowing to add mate between two co-ordinate syatems.

          You can  add mate between co-ordinate and sketch point or in between sketch points.

           

          Like below one

           

          boolstatus = Part.Extension.SelectByID2("Point1@Origin@T1431521-1@Assem1", "EXTSKETCHPOINT", 0, 0, 0, True, 1, Nothing, 0)

          boolstatus = Part.Extension.SelectByID2("Coordinate System1", "COORDSYS", 0, 0, 0, True, 1, Nothing, 0)

          Dim myMate As Object

          Set myMate = Part.AddMate5(20, -1, False, 0.234637476354974, 0.001, 0.001, 0.001, 0.001, 0.5235987755983, 0.5235987755983, 0.5235987755983, False, False, 0, longstatus)

            • Re: Macro to mate named coordinatesystem
              Viktor Lundström

              Akshay Abitkar skrev:

               

              Solidworks UI does not allowing to add mate between two co-ordinate syatems.

              You can add mate between co-ordinate and sketch point or in between sketch points.    

               

              Hi,

               

              Thank you for your reply.

              I don't believe you, you can absolutely mate two COORDSYS together. (maybe i was describing badly in my first post? :).

               

              I have managed to get the code beneath to work, however there is on crucial part missing.

              This only works for the instance "CoordName (1)" with the statically selected "CompName" & "CompName2".

              I would like someone to help me figure out the best way to "FIND" these common "COORDSYS" that i have defined in the components.

              INF_KST_ORA_SVETS

              INF_KST_BOTTEN

              INF_KST_ORA

              etc.

               

              If i manage to find them i probably can automate all my assembling. ( How great wouldn't that be? )

               

              Feel free to ask anything if it can help the subject.

              /Viktor

               

               

               

              'Sub Add_Mates(SWAsmName As String, SWCompName As String, SWAsm As SldWorks.AssemblyDoc)

              Dim swApp               As SldWorks.SldWorks

              Dim swModel             As SldWorks.ModelDoc2

              Dim swDocExt            As SldWorks.ModelDocExtension

              Dim SWMateFeat          As SldWorks.Feature

              Dim Asm                 As SldWorks.AssemblyDoc

              Dim MateName            As String

              Dim FirstSelection      As String

              Dim SecondSelection     As String

              Dim CompName            As String

              Dim CompName2           As String

              Dim AsmName             As String

              Dim AsmName2            As String

              Dim AsmName3            As String

              Dim AsmName4            As String

              Dim SubAsmName          As String

              Dim SubAsmName2         As String

              Dim CoordName(50)        As String

              Dim boolstat            As Boolean

              Dim SWMateError         As Long

              Dim Str                 As Variant

              Dim partnum(8)          As String

              Dim x                   As Integer

               

               

              Sub Add_CoordMate()

               

               

              Set swApp = CreateObject("SldWorks.Application")

              Set swModel = swApp.ActiveDoc

               

               

              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 (swModel.GetType = swDocASSEMBLY) Then

                      MsgBox "En sammanställning (.sldasm) måste vara öppen för att köra detta kommando!"

                 

                  Exit Sub

                  End If

                 

              CoordName(1) = "INF_KST_BOTTEN"

              'CoordName(2) = "INF_KST_KOLV"

              'CoordName(3) =INF_BOTTEN_ROR

              CompName = "51520-00 Kolvstång 63 32 A-1"

              CompName2 = "105-1006 Botten plan 63-1"

               

               

               

               

              AsmName2 = swModel.GetTitle

              Debug.Print AsmName2

              Str = Split(AsmName2, ".")

              AsmName = Str(0)                                'Assembly name always 10 digits ending with 01

              Debug.Print AsmName

              Set Asm = swModel

              SubAsmName_inst1 = Left(AsmName, 9) & "8-1"     'Sub Assembly name and instance 1

              SubAsmName1 = Left(SubAsmName_inst1, 10)        'Sub Assembly name 1 (always 10 digits ending with 08)

              SubAsmName_inst2 = Left(AsmName, 9) & "9-1"     'Sub Assembly name and instance 2

              SubAsmName2 = Left(SubAsmName_inst2, 10)        'Sub Assembly name 2 (always 10 digits ending with 09)

               

              For x = 1 To 1                                  'depending on number of CoordName instances

               

              MateName = "Coincident_" + CoordName(x)

               

              'FirstSelection = "INF_KST_BOTTEN@0665733908-1@0665733901/51520-00 Kolvstång 63 32 A-1@0665733908"

              FirstSelection = CoordName(x) & "@" + SubAsmName_inst1 & "@" + AsmName & "/" + CompName & "@" + SubAsmName1

              Debug.Print FirstSelection

              'SecondSelection = "INF_KST_BOTTEN@0665733909-1@0665733901/105-1006 Botten plan 63-1@0665733909"

              SecondSelection = CoordName(x) & "@" + SubAsmName_inst2 & "@" + AsmName & "/" + CompName2 & "@" + SubAsmName2

              Debug.Print SecondSelection

               

              swModel.ClearSelection2 (True)

               

              Set swDocExt = swModel.Extension

               

              'Select entities for mating

              boolstat = swDocExt.SelectByID2(FirstSelection, "COORDSYS", 0, 0, 0, True, 1, Nothing, swSelectOptionDefault)

              boolstat = swDocExt.SelectByID2(SecondSelection, "COORDSYS", 0, 0, 0, True, 1, Nothing, swSelectOptionDefault)

               

              'Add the mate

              Set SWMateFeat = Asm.AddMate3(20, swMateAlignALIGNED, False, 0, 0, 0, 0, 0, 0, 0, 0, False, SWMateError)

              SWMateFeat.Name = MateName

               

               

              Next

              End Sub