8 Replies Latest reply on Jun 14, 2017 9:12 AM by Michael Halbleib

    API Help!  Creating a derived mirror configuration.

    Michael Halbleib

      Hello, I am making the switch from lurking on the forums to asking a question.  The reason for this change is that I cannot find an answer to my problem anywhere I have looked.  Sorry if this is a simple request, but I have no formal training in making macros.

       

      My problem is that I am trying to make a macro to created a derived mirror configuration of an open part file.  Manually we always open an assembly, insert the part and run the mirror function, ticking off the same selections each time.  I did not see any simpler existing way in SolidWorks 2016 so I decided to write a macro to do it.  Below is the code I have so far, however, I continually get a "Run-time error '438': Object doesn't support this property or method"  What am I doing wrong, what needs changed to make this work, or is there a simpler way to get the same result?  Thanks for any help.

       

      Dim swPart As Object
      Dim swAssy As Object
      Dim Result As Long
      Dim ActiveConfig As Object
      Dim boolstatus As Boolean
      Dim errors As Long
      Dim warnings As Long

       

      Public Sub CreateMirror()
          Set swApp = Application.SldWorks
          swApp.Visible = True
          Set swDoc = swApp.ActiveDoc

       

          Set swPart = swApp.ActiveDoc
          Set ActiveConfig = swPart.GetActiveConfiguration

          Dim ConfigName As String
              ConfigName = ActiveConfig.Name
          Dim PartPathName As String
              PartPathName = swPart.GetPathName()
              PartPath = InStrRev(PartPathName, "\") + 1
              PartExten = InStrRev(PartPathName, ".")
          Dim PartName As String
              PartName = Mid$(PartPathName, PartPath, PartExten - PartPath)


          Set swDoc = swApp.NewDocument("C:\WTI-Vault\WTI Templates\Assembly.asmdot", 0, 0, 0)
          Set swAssy = swApp.ActiveDoc
          Dim AssyName As String
              AssyName = swAssy.GetTitle


          boolstatus = swAssy.AddComponent(PartPathName, 0, 0, 0)


          Dim MirrorPlane As SldWorks.Feature
          Set MirrorPlane = swAssy.FeatureByName("Right Plane")


          Dim CompToMirror(0) As Object
          Set CompToMirror(0) = swAssy.GetComponentByName(PartName & "-1")


          Dim NameModifier As String
              NameModifier = ""

       

          Dim DerivedName As String
              DerivedName = "_Mirror2"


          Dim CompToInstance As Object
          Set CompToInstance = Nothing

       

          Dim Orientations As Object
          Set Orientations = Nothing

       

          Dim OrientAboutCoM As Boolean
              OrientAboutCoM = False

       

          Dim CreateDerivedConfigs As Boolean
              CreateDerivedConfigs = True

       

          Dim NameModifierType As swMirrorComponentNameModifier_e
              NameModifierType = swMirrorComponentName_Custom

       

          Dim FileLocation As String
              FileLocation = Left$(PartPathName, PartPath)

       

          Dim ImportOptions As Integer
              ImportOptions = swMirrorPartOptions_ImportSolids

       

          Dim BreakLinks As Boolean
              BreakLinks = False

       

          Dim PreserveZAxis As Boolean
              PreserveZAxis = False

       

          Dim FlexSync As Boolean
              FlexSync = False

       

          Dim DerivedMirror As Object
              DerivedMirror = swAssy.MirrorComponents3(MirrorPlane, CompToInstance, Orientations, OrientAboutCoM, _
                              CompToMirror, CreateDerivedConfigs, DerivedName, NameModifierType, NameModifier, _
                              FileLocation, ImportOptions, BreakLinks, PreserveZAxis, FlexSync)

        
          swApp.CloseDoc AssyName

          boolstatus = swPart.EditRebuild3()

       

      End Sub

        • Re: API Help!  Creating a derived mirror configuration.
          Matt Martens

          Michael,

           

          Many of us here on the forums are also self taught. The easiest way to learn is to just step in and try.

           

          For your question, at what point does the error occur? If you're not sure, try stepping through your code by pressing F8 until you reach the error. The other thing you can do is press "Debug" in the error dialog. That will keep the macro running and highlight the line where the code is currently, which will also most likely be where the error occurs.

           

          I copied your code and the error occurs for me at

          boolstatus = swAssy.AddComponent(PartPathName, 0, 0, 0)

           

          This is occurring for me because the new assembly document specified 3 lines earlier doesn't actually open so on the line after that

          Set swAssy = swApp.ActiveDoc

          is actually setting swAssy as the current part file that is open. Obviously you can't insert a component into a part, only an assembly.

           

          One thing of note on your code, it is better when declaring your variables to set them as the actual SW object you expect them to be. So instead of

          Dim swPart As Object

          you would have

          Dim swPart As SldWorks.PartDoc or

          Dim swPart As SldWorks.ModelDoc2

           

          and

          Dim swAssy As Object

          would be

          Dim swAssy As SldWorks.AssemblyDoc

          Dim swAssy As SldWorks.ModelDoc2

           

          That way if you aren't getting the expected SW object it will error at that point instead of further down the line when you try to do something with that object that isn't a part of the API for that object.

           

          Also, I created an SPR a couple of years ago to allow a user to create a mirrored derived configuration of a part without having to insert it into an assembly. It is SPR #838809. Here is the link:

          https://customerportal.solidworks.com/eservice_enu/start.swe?SWECmd=InvokeMethod&SWEMethod=GotoRecord&SWEService=SWGotoRecord&ViewName=SW+Defect+ER+Search+List+View+(eService)&BusObject=Product+Defect&BusComp=Product+Defect&Id=1-364DURJ&SRN=

           

          Good luck and happy programming.

            • Re: API Help!  Creating a derived mirror configuration.
              Michael Halbleib

              Thank you Matt.  First, I only get a blank page why I tried to follow your link, I tried in both Chrome and IE.  For now, I tried your suggestions of defining swPart and swAssy as PartDoc and AssemblyDoc respectively, I like that it will throw an error if they are not what is expected.  I still get the same error I got before, it occurs near the end of the code at the DerivedMirror = ...  For why it does not work for you, I realize the coding I have would not work for anyone else unless you replace the Set swDoc = swApp.NewDocument("..." with the file location and name of your assembly template.  Hopefully this helps identifying the problem I have with the code.

            • Re: API Help!  Creating a derived mirror configuration.
              Matt Martens

              At the beginning of that line begin with "Set DerivedMirror = ..."

               

              When you are using an object variable you must use set when using it. Or, since you aren't using the DerivedMirror variable for anything you can have the line as

              swAssy.MirrorComponents3 MirrorPlane, CompToInstance, Orientations, OrientAboutCoM, _

                                      CompToMirror, CreateDerivedConfigs, DerivedName, NameModifierType, NameModifier, _

                                      FileLocation, ImportOptions, BreakLinks, PreserveZAxis, FlexSync

               

              When executing commands in code you can do it this way if you don't need to capture the result for further use. The same goes for booleans, strings, and any other type of variable. I only do the "=" syntax if I need to capture the result so I don't have to declare a bunch of variables that I have no intention of using.

               

              If your swAssy.MirrrorComponents3 options are always the same, you can just use the equivalent from earlier in your code in this line. Even though the code runs through that section fast it is unnecessary.

                • Re: API Help!  Creating a derived mirror configuration.
                  Michael Halbleib

                  Matt, I tried your recommendations and I am still getting the error.  Could it be a SolidWorks setting instead of an issue with my code?  Or maybe a missing reference.

                    • Re: API Help!  Creating a derived mirror configuration.
                      Matt Martens

                      I think maybe the issue may be your MirrorPlane object. The API help says that argument must be an IRefPlane object or an IFace object. The way you are capturing that variable I believe you are ending up with an IFeature object. You can use IFeature::GetSpecificFeature2 to capture the IRefPlane object to a variable or you could use MirrorPlane.GetSpecificFeature2 as the argument for MirrorComponents3.

                        • Re: API Help!  Creating a derived mirror configuration.
                          Michael Halbleib

                          Not sure If I implemented it correctly, but below is what I changed and I STILL get the same error at the end.  I changed MirrorPlane to Instance and made MirrorPlane the new value.  I don't think it made any difference, or I'm using it wrong.

                           

                              Dim Instance As SldWorks.Feature

                              Set Instance = swAssy.FeatureByName("Right Plane")

                              Dim MirrorPlane As SldWorks.RefPlane

                              Set MirrorPlane = Instance.GetSpecificFeature2()

                            • Re: API Help!  Creating a derived mirror configuration.
                              Matt Martens

                              Well, I have been able to get the mirror command working but have been unable to get it to create the mirrored configuration.

                              I used:

                               

                              Dim MirrorPlane As SldWorks.RefPlane

                              Set MirrorPlane = swAssy.FeatureByName("Right Plane").GetSpecificFeature2

                              swAssy.MirrorComponents3 MirrorPlane, Nothing, Nothing, True, (CompToMirror), True, "", _

                                                        swMirrorComponentNameModifier_e.swMirrorComponentName_Custom, _

                                                       "_Mirror2", "", swMirrorPartOptions_ImportSolids, False, False, False

                               

                              I have been messing with it for 30 minutes or and I can't get it to click the "Create opposite hand version". Give it a whirl and keep at it. Eventually you may get it.

                               

                              I'm checking out for the weekend so good luck.