4 Replies Latest reply on Mar 28, 2014 3:59 PM by Patrick O'Hern

    Unwanted sketch included in InsertNewVirtualPart

    Gregory Sharp



      I'm using this piece of code, simplified a bit, in a macro:



          Dim swSelMgr As SldWorks.SelectionMgr

          Set swSelMgr = swModel.SelectionManager



                If NamePart.optTop.Value = True Then


                    partplane = "Top"




               If NamePart.optFront.Value = True Then


                    partplane = "Front"




               If NamePart.optRight.Value = True Then


                    partplane = "Right"


               End If

               End If

               End If



          boolstatus = swModel.Extension.SelectByID2(partplane, "PLANE", 0, 0, 0, False, 0, Nothing, 0)


          Dim swPlaneFeature As SldWorks.Feature

          Set swPlaneFeature = swSelMgr.GetSelectedObject6(1, -1)

          Dim swPlane As SldWorks.RefPlane

          Set swPlane = swPlaneFeature.GetSpecificFeature2


          Dim swAssem As SldWorks.AssemblyDoc

          Set swAssem = swModel


          lResult = swAssem.InsertNewVirtualPart(swPlane, swVirtComp)


      The issue is, when the virtual part is inserted, it already contains a sketch, located on the "Front" plane. When I perform the "insert component" operation manually, it doesn't contain a sketch. Any idea why a sketch is being created by that method?

        • Re: Unwanted sketch included in InsertNewVirtualPart
          Drew Adams

          My guess is because when you insert a new virtual component in core SolidWorks, the first thing you're asked to do is to make a selection to define the virtual component's Front Plane.  If you select something that could be considered a valid sketch plane, it puts you directly into a new sketch. 

          I'm going out on a limb here, but I'd assume that when you use the method with a defined and valid sketch plane, it follows that same logic and places you into a new sketch.   Somebody who is a little more familiar with the API could probably clear this up further, but that's my best educated guess.



            • Re: Unwanted sketch included in InsertNewVirtualPart
              Gregory Sharp

              Thanks Drew,


              I find that when I insert a virtual part manually, simply by selecting "new part" in the Insert\Components drop-down, it defaults to inserting the part at the front plane, sets the virtual part as fixed, and doesn't include a sketch. In other words, it's not necessary to select a plane before or during the operation.

                • Re: Unwanted sketch included in InsertNewVirtualPart
                  Jeremy Feist

                  no, in the manual method it is not necessary to select a plane, but it is necessary to click somewhere. if you click on a plane or planar surface/face, it starts a sketch and adds an "inplace" mate (and that's the only way i have found to add that mate). if you click on the "ok" check mark, or an empty area of the graphics window, it does not start a sketch, aligns the part planes to the assembly planes, and fixes the part.


                  so, if you can modify the code to not select a plane, I think you can avoid the sketch - but I have no real experience with the API.

                  • Re: Unwanted sketch included in InsertNewVirtualPart
                    Patrick O'Hern

                    Rather than inserting it as a virtual component, you can insert as a regular component, and then convert it to virtual.  Here is some code from a macro that I have that does just that (my code inserts an existing file, so I've added code at the beginning to create a new document first)


                         strTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplatePart)

                         swModelComp = swApp.NewDocument(strTemplate,0,0,0)



                            'Save selected component as a new file

                                bStatus = swModelComp.Extension.SaveAs(strPathTemp, swSaveAsCurrentVersion, swSaveAsOptions_Silent, _

                                    Nothing, lngErrors, lngWarnings)

                            'Add new component to assembly

                                Set swCompNew = swAssy.AddComponent5(strPathTemp, swAddComponentConfigOptions_CurrentSelectedConfig, _

                                    "", False, "", 0, 0, 0)

                            'Close component file

                                swApp.CloseDoc (strPathTemp)


                            'Set new component as Virtual

                                bStatus = swCompNew.MakeVirtual


                            'Update name of virtual component

                                strCompName = swCompNew.Name2

                                strCompName = Replace(strCompName, "Copy of ", "")


                            'Rename virtual component

                                swCompNew.Name2 = strCompName


                            'Delete temporary file

                                Kill strPathTemp