2 Replies Latest reply on Dec 22, 2014 12:55 PM by Drew Miller

    Edit a sketch in a new virtual part

    Drew Miller

      Hello-  using vba & sw2013.  Currently macro successfully inserts a new virtual part into an assembly in a selected face from a pmp selectionbox.    Need to be able to add some geometry to that blank new part- which contains "Sketch1", basically to create a rectangular sketch on sketch1.  I am unable to edit the virtual component Sketch1 feature.  Here's what I've got so far- have tried a couple ways to select and edit the sketch. 

       

      ' Create the part and insert it as a virtual component in the assembly

              Status = swAssy.InsertNewVirtualPart(swFeature, swComponent)  ' this works

              Status = swComponent.Select(False)

             

              Set swModelForEdit = swComponent.GetModelDoc2()  ' way to get modeldoc from a component object

              Set swConf = swModelForEdit.GetActiveConfiguration() ' gets the virtual part's active configuration

              Debug.Print swComponent.Name + " " + swConf.Name  'output the name and active config of the inserted part

               TraverseFeatures swComponent.FirstFeature  'must pass the component's first feature to begin traversing features, this outputs the feature names in the output window, works ok

       

         '***********'

              statInfo = swAssy.EditPart2(True, False, info)

              Set swEditModel = swAssy.GetEditTarget()

              Set swEditPart = swEditModel

      '        Set swFeat = swEditPart.FeatureByName("Sketch1") '.FeatureByName("Sketch1")

      '        Status = swFeat.Select2(False, -1)

              Status = swEditModel.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)

              swEditModel.EditSketch

              '************'

        • Re: Edit a sketch in a new virtual part
          Daniel Andersson

          Hi Drew Miller,

           

          I tried using the macro recorder and got some code that I stripped down and slightly modified to what is needed.

          The code requires a assembly name with Assem1 and a virtual part with name Part1^Assem1. In the virtual part there is an existing sketch with name Sketch1. Note that the SelectByID2 uses the instance of the component as well for selection, hence the added -1 to the virtual part name.

           

          Hope this helps you to get forward. There might be other options as going down the road with the IModelDoc2 for the virtual part, but this looks to be a quite straight forward way.

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

           

          Dim boolstatus As Boolean

           

          Sub main()

               Set swApp = Application.SldWorks

               Set swModel = swApp.ActiveDoc

           

               swModel.ClearSelection2 True

               boolstatus = swModel.Extension.SelectByID2("Sketch1@Part1^Assem1-1@Assem1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)

               swModel.EditSketch

           

               Dim skSegment As Object

               Set skSegment = swModel.SketchManager.CreateCircle(-0#, 0#, 0#, 0.00634, 0.013022, 0#)

           

               swModel.SketchManager.InsertSketch True

               swModel.EditAssembly

           

          End Sub

            • Re: Edit a sketch in a new virtual part
              Drew Miller

              Thanks, that worked. I was making it more difficult than necessary- I was thinking that you needed to edit the modeldoc2 of the part, but you are actually editing the modeldoc2 of the assembly after the in context part is activated.

               

               

              Found this in the api help:

                 'Re:  GetEditTarget: This method is useful when the user is performing an in-context edit of a part component because the currently active document returned from ISldWorks::ActiveDoc might not be the document that the user is editing. For example, the user can use in-context editing to modify an assembly component. The active document is the assembly, but this method returns the IModelDoc2 for the component being edited.

                      'The IModelDoc2 object returned by this method might be that of the assembly if no part is being in-place edited. In other words, this method returns a valid pointer (non-NULL) even when user is not editing a part in-context.

                      'You can use the IModelDoc2 object returned by this method to determine which assembly component is being edited in-context. In general, you should not use this IModelDoc2 object for feature creation within the component part. Feature creation typically requires the IModelDoc2 of the active document. During feature creation, SolidWorks automatically determines whether the feature should be created and owned by the active assembly, or if it is an in-context edit in which the feature should be created and owned by the component part.

               

              Appreciate your help

               

              Drew