6 Replies Latest reply on Jul 17, 2014 1:12 PM by Alice Caspari

    Mating Macro

    Alice Caspari

      I am trying to use a macro to mate two planes, but at the point the macro will run the component currently referred to as "Base Assmbly-1@POWERPACK" will be an unknown assembly that will have a "BAFRONTPLANE" and a "BATOPPLANE" in it. Any ideas?

       

      Option Explicit

      ' ******************************************************************************
      ' Use to Mate Base Assembly Planes 07/17/14 by CASPARIA
      ' ******************************************************************************

      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("Front", "PLANE", 0, 0, 0, True, 1, Nothing, 0)
      boolstatus = Part.Extension.SelectByID2("BAFRONTPLANE@Base Assembly-1@POWER PACK", "PLANE", 0, 0, 0, True, 1, Nothing, 0)
      Dim myMate As Object
      Set myMate = Part.AddMate3(0, 0, False, 0, 0, 0, 0.001, 0.001, 0, 0.001, 0.001, False, longstatus)
      Part.ClearSelection2 True
      Part.EditRebuild3

      boolstatus = Part.Extension.SelectByID2("Top", "PLANE", 0, 0, 0, True, 1, Nothing, 0)
      boolstatus = Part.Extension.SelectByID2("BATOPPLANE@Base Assembly-1@POWER PACK", "PLANE", 0, 0, 0, True, 1, Nothing, 0)
      Set myMate = Part.AddMate3(0, 0, False, 0, 0, 0, 0.001, 0.001, 0, 0.001, 0.001, False, longstatus)
      Part.ClearSelection2 True
      Part.EditRebuild3

      End Sub

        • Re: Mating Macro
          Deepak Gupta

          Basically the macro is selecting the planes of a subassembly. Will the top level assembly name will be same OR that is also unknown?

            • Re: Mating Macro
              Alice Caspari

              Correct Deepak

              To give some background, I am automating some assemblies for a configurator. This macro will run after several subassemblies and parts (unknown names) are inserted into an assembly file. The subassembly that will always be first in the tree will contain the 2 names planes. At the time this macro will be triggered the Assembly file will be called Power Pack, the Base Assembly-1 will be a different unknown name.

                • Re: Mating Macro
                  Deepak Gupta

                  So will you be pre-selecting the sub-assembly to mate? I mean if there are several sub-assemblies then how the macro decide to pick the required sub-assembly?

                    • Re: Mating Macro
                      Alice Caspari

                      I need to have it select the first subassembly in the tree.

                      Basically I need a way to Get the name of the first component in the tree.

                        • Re: Mating Macro
                          Deepak Gupta

                          I'm not that good at APIs but here is what I had madeusing codes from the API help files. And they work too

                           

                          Option Explicit

                          Dim sChildName As String

                          Dim longstatus As Long, longwarnings As Long

                          Sub TraverseComponent(swComp As SldWorks.Component2, nLevel As Long)

                           

                              Dim vChildComp               As Variant

                              Dim swChildComp            As SldWorks.Component2

                              Dim swCompConfig          As SldWorks.Configuration

                              Dim sPadStr                    As String

                              Dim i                               As Long

                             

                              vChildComp = swComp.GetChildren

                              For i = 0 To 0

                                  Set swChildComp = vChildComp(i)

                                  sChildName = swChildComp.Name2

                              Next i

                          End Sub

                           

                          Sub main()

                           

                              Dim swApp                       As SldWorks.SldWorks

                              Dim swModel                    As SldWorks.ModelDoc2

                              Dim swConf                      As SldWorks.Configuration

                              Dim swRootComp              As SldWorks.Component2

                              Dim bRet                           As Boolean

                           

                           

                              Set swApp = CreateObject("SldWorks.Application")

                              Set swModel = swApp.ActiveDoc

                              Set swConf = swModel.GetActiveConfiguration

                              Set swRootComp = swConf.GetRootComponent3(True

                           

                              TraverseComponent swRootComp, 1   

                             

                              swModel.Extension.SelectByID2 "Front", "PLANE", 0, 0, 0, True, 1, Nothing, 0

                              swModel.Extension.SelectByID2 "BAFRONTPLANE@" & sChildName & "@" & swModel.GetTitle, "PLANE", 0, 0, 0, True, 1, Nothing, 0

                              swModel.AddMate3 0, 0, False, 0, 0, 0, 0.001, 0.001, 0, 0.001, 0.001, False, longstatus

                              swModel.ClearSelection2 True

                             

                              swModel.Extension.SelectByID2 "Top", "PLANE", 0, 0, 0, True, 1, Nothing, 0

                              swModel.Extension.SelectByID2 "BATOPPLANE@" & sChildName & "@" & swModel.GetTitle, "PLANE", 0, 0, 0, True, 1, Nothing, 0

                              swModel.AddMate3 0, 0, False, 0, 0, 0, 0.001, 0.001, 0, 0.001, 0.001, False, longstatus

                              swModel.ClearSelection2 True

                             

                              swModel.ForceRebuild3 True

                                 

                          End Sub

                           

                          May be some expert can refine it further.