7 Replies Latest reply on Nov 4, 2016 5:50 AM by Aaron Heathcote

    AddComponent5 Not Fixed to Origin

    Aaron Heathcote

      I am building a standalone application in vb.net and I am having some difficulties adding a component in the correct position.

      InsertComponent.png

      When adding a part to an assembly within SolidWorks, if you just hit the green tick then the component will be fixed in position (front plane of part to front plane of assembly, top plane of part to top plane of assembly and then right plane of part to right plane of assembly).

      This is what I am trying to achieve but when I use AddComponent5 then the part I am putting into the assembly is fixed by the centre of the part to the origin of the assembly instead of the origin of part to origin of assembly.

      PartInserted.png

      As you can see above, the top origin is the origin of the assembly and the bottom is the origin of the part & the centre of the part is fixed to the assembly origin.

       

      My Code:

              'Make Assemblies

              Dim Asm As IModelDoc2

              Dim AsmDoc As IAssemblyDoc

              Dim InsertComponent As Component2

              Dim asmTemplate As String

              asmTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplateAssembly)

              Asm = CType(swApp.NewDocument(asmTemplate, 0, 0, 0), ModelDoc2)

              Asm = CType(swApp.ActiveDoc, ModelDoc2)

              AsmDoc = Asm

       

              Dim Post1Part As String

              Post1Part = A123456-1111-1@Assem1

       

       

              InsertComponent = AsmDoc.AddComponent5(Form2.FileLocation & Form2.JobNumber & "-" & Form2.LineNumber & "111.SLDPRT", swAddComponentConfigOptions_e.swAddComponentConfigOptions_CurrentSelectedConfig, "", False, "", 0, 0, 0)

              Asm.Extension.SelectByID2(Post1Part, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)

              AsmDoc.UnfixComponent()

              Asm.ClearSelection2(True)

       

      The reason I have used AddComponent5 is because it allows the insert of components without having the components open, maybe there is another method?

      Also I know that I have specified the position by defining the coordinates as 0,0,0 but what I'm wondering is is there a better way to insert the components and have them mated to the origin?


      Currently my code inserts the part, then floats it, then my next move would be to mate front to front, top to top and right to right. Seems like unnecessary code but couldn't find a way to make this automatic on import.

       

      Any help would be greatly appreciated.

        • Re: AddComponent5 Not Fixed to Origin
          Peter Brinkhuis

          I agree that is weird that it uses the center of the part instead of the origin. The help even confesses this:

          The x, y, and z parameters of this method are relative to the bounding box of the component. Only use this method if you want to approximately position the component. Use IComponent2::Transform2 if you want to more precisely position the component.

          So I guess you shouldn't be using AddComponent5. Why would anybody want this, yuk.

           

          Have you tried transforming it immediately after adding the part? Or just adding an origin-coincident mate?

          • Re: AddComponent5 Not Fixed to Origin
            Deepak Gupta

            I had used AddComponent5 sometime back and inserted parts were not fixed in the place. So I used the AddMate5 with "ForPositioningOnly" set to true.

             

            This means, you can select planes of assembly and inserted part, mate them using the for position only option and then fix that part. Don't have the macro codes as I scrapped them for some other reason but this should not be too complex.

             

             

              • Re: AddComponent5 Not Fixed to Origin
                Aaron Heathcote

                Peter Brinkhuis

                 

                Thanks both for your feedback, I've decided to go with Deepaks suggestion and use AddMate5 then fix the component which is good for this scenario.

                My next problem is within another assembly and getting the Symmetric Mate to work with AddMate5.

                 

                My Code:

                Asm.Extension.SelectByID2("Right Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)

                        Asm.Extension.SelectByID2("Right Plane@1110-1@Assem3", "PLANE", 0, 0, 0, True, 0, Nothing, 0)

                        Asm.Extension.SelectByID2("Right Plane@1120-1@Assem3", "PLANE", 0, 0, 0, True, 0, Nothing, 0)

                        myMate = CType(AsmDoc.AddMate5(8, 1, False, 0, 0, 0, 0, 0, 0, 0, 0, False, False, 0, 0), Mate2)

                        Asm.ClearSelection2(True)

                 

                This code will select all 3 Right Planes and then attempt to do a symmetric mate although this is not working. I've tried to change the selection order, the alignment and For Positioning Purpose and none have worked, Distance mate works fine if I were to use that twice but its really not a good option in this scenario.

                Have either of you guys gotten to symmetric mate to work?

                  • Re: AddComponent5 Not Fixed to Origin
                    Deepak Gupta

                    Never tried symmetric mate via API but try recording a macro for it and see if that helps you figure out something.

                    • Re: AddComponent5 Not Fixed to Origin
                      Josh Brady

                      Have you looked at the documentation?   You are using Mark = 0 in SelectByID2.

                       

                      Per the help file,

                       

                      To add a mate:

                       

                      1. Call IModelDoc2::ClearSelection2 before selecting entities to mate.

                      2. Call IModelDocExtension::SelectByID2 to select each entity to mate.

                        If MateTypeFromEnum is...Use selection mark...
                        swMateType_e.swMateCAMFOLLOWER8
                        swMateType_e.swMateWIDTH16
                        Other swMateType_e option1
                      3. Call this method.

                      4. Call IModelDoc2::ClearSelection2 after the mate is created.

                        • Re: AddComponent5 Not Fixed to Origin
                          Aaron Heathcote

                          Hi Josh, thanks for helping.

                          I have tried using 1 for Mark but couldn't get it to work either.

                           

                          I've been dissecting the code and here is what I found.

                           

                          My Code:

                          Asm.ClearSelection2(True)

                                  'Symmetric Mate Between Two Posts - NOT WORKING

                                  Asm.Extension.SelectByID2("Right Plane@" & Form2.JobNumber & "-" & Form2.LineNumber & "110-1@Assem3", "PLANE", 0, 0, 0, True, 1, Nothing, 0)

                                  Asm.Extension.SelectByID2("Right Plane@" & Form2.JobNumber & "-" & Form2.LineNumber & "120-1@Assem3", "PLANE", 0, 0, 0, True, 1, Nothing, 0)

                                  Asm.Extension.SelectByID2("Right Plane", "PLANE", 0, 0, 0, True, 1, Nothing, 0)

                                  myMate = CType(AsmDoc.AddMate5(8, 0, False, 0, 0, 0, 0, 0, 0, 0, 0, False, True, 0, 0), Mate2)

                                  Asm.ClearSelection2(True)

                           

                           

                          First of all we clearing any selections

                           

                          Then we are selecting the Right Plane of the Assembly (Because the plane is not already selected the plane is appended to the current selection list, Mark is set to 1 as per the documentation, nothing is selected for the call out and any errors will return a default error)

                           

                          Then we are selecting the Right Plane of the Assembly 1110 (Because the plane is not already selected the plane is appended to the current selection list - therefore there are two planes currently selected, Mark is set to 1 as per the documentation, nothing is selected for the call out and any errors will return a default error)

                           

                          Then we are selecting the Right Plane of the Assembly 1120 (Because the plane is not already selected the plane is appended to the current selection list - therefore there are three planes currently selected, Mark is set to 1 as per the documentation, nothing is selected for the call out and any errors will return a default error)

                           

                          Then we are adding a mate (Symmetric, aligned, not flipped, not distance needed, not required, not required, not required, not required, not required, not required, not required, Position is set to false because we want to return a mate, Rotation is set to true because we want the component to be locked, width mate isn't needed so used 0, used 0 as a default error)

                           

                          Then Clear Selection

                           

                          When i run this line by line it selects all of the planes as desired but the execution of the mate doesn't work. So next I've tried changing the order of the selections to see if this makes a difference as it does when selecting a symmetric mate within SolidWorks manually.

                           

                          Selecting Assembly plane first doesn't work:

                          Asm.ClearSelection2(True)

                                  'Symmetric Mate Between Two Posts - NOT WORKING

                                  Asm.Extension.SelectByID2("Right Plane", "PLANE", 0, 0, 0, True, 1, Nothing, 0)

                                  Asm.Extension.SelectByID2("Right Plane@" & Form2.JobNumber & "-" & Form2.LineNumber & "110-1@Assem3", "PLANE", 0, 0, 0, True, 1, Nothing, 0)

                                  Asm.Extension.SelectByID2("Right Plane@" & Form2.JobNumber & "-" & Form2.LineNumber & "120-1@Assem3", "PLANE", 0, 0, 0, True, 1, Nothing, 0)

                                  myMate = CType(AsmDoc.AddMate5(8, 0, False, 0, 0, 0, 0, 0, 0, 0, 0, False, True, 0, 0), Mate2)

                                  Asm.ClearSelection2(True)

                           

                          Selecting component plane, assembly plane and then component2 plane doesn't work either.

                                  Asm.ClearSelection2(True)

                                  'Symmetric Mate Between Two Posts - NOT WORKING

                                  Asm.Extension.SelectByID2("Right Plane@" & Form2.JobNumber & "-" & Form2.LineNumber & "110-1@Assem3", "PLANE", 0, 0, 0, True, 1, Nothing, 0)

                                  Asm.Extension.SelectByID2("Right Plane", "PLANE", 0, 0, 0, True, 1, Nothing, 0)

                                  Asm.Extension.SelectByID2("Right Plane@" & Form2.JobNumber & "-" & Form2.LineNumber & "120-1@Assem3", "PLANE", 0, 0, 0, True, 1, Nothing, 0)

                                  myMate = CType(AsmDoc.AddMate5(8, 0, False, 0, 0, 0, 0, 0, 0, 0, 0, False, True, 0, 0), Mate2)

                                  Asm.ClearSelection2(True)

                           

                          Sorry for the long post, I've been using it to sort of document what I have already tried and unfortunately I haven't found the answer yet (very frustrating).

                          If anyone sees what I'm doing wrong please let me know and If i discover a way to do this I will come back and share.