Hide Table of Contents

Move Copy Sketch Entities Example (VB.NET)

This example shows how to move, copy, and move and copy sketch entities.

' Preconditions: Verify that the specified part template exists.
' Postconditions:
' 1. Insert a breakpoint at the following lines of code:
'    swFeature = swPart.FeatureByName("Sketch1")
'    swModel.EditUndo2(1)
' 2. Run the macro.
' 3. Examine the sketch at each breakpoint, then press F5.
'     1.  Opens a new part document.
'     2.  Opens a sketch and sketches a line and a circle.
'     3.  Selects the line and circle.
'     4.  Moves the line and circle.
'     5.  Moves and copies the line and circle once.
'     6.  Moves and copies the line and circle twice.
' 4. Close the part document without saving it.
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System.Runtime.InteropServices
Imports System
Partial Class SolidWorksMacro
    Public Sub main()
        Dim swModel As ModelDoc2
        Dim swModelDocExt As ModelDocExtension
        Dim swPart As PartDoc
        Dim swFeature As Feature
        Dim swSketchMgr As SketchManager
        Dim swSketchSegment As SketchSegment
        Dim lIdx As Integer
        Dim bCopy As Boolean
        Dim lNumCopies As Integer
        Dim aBasePoint(2) As Double
        Dim aMoveVector(2) As Double
        Dim errors As Integer
        Dim status As Boolean
        ' Open a new part document and sketch a line and a circle
        swModel = swApp.NewDocument("C:\ProgramData\SOLIDWORKS\SOLIDWORKS 2013\templates\Part.prtdot", 0, 0, 0)
        swApp.ActivateDoc3("Part1"TrueFalse, errors)
        swModel = swApp.ActiveDoc
        swPart = swModel
        swModelDocExt = swModel.Extension
        swSketchMgr = swModel.SketchManager
        swSketchSegment = swSketchMgr.CreateLine(-0.096389, 0.032667, 0.0#, -0.062943, 0.019437, 0.0#)
        swSketchSegment = swSketchMgr.CreateCircle(-0.084504, 0.013823, 0.0#, -0.087932, 0.006083, 0.0#)
        swFeature = swPart.FeatureByName("Sketch1")
        status = swFeature.Select2(False, 0)
        aBasePoint(0) = 0.0#
        aBasePoint(1) = 0.0#
        aBasePoint(2) = 0.0#
        aMoveVector(0) = 0.1
        aMoveVector(1) = 0.0#
        aMoveVector(2) = 0.0#
        For lIdx = 0 To 2
            status = swModelDocExt.SelectByID2("Line1""SKETCHSEGMENT", -0.0752087432116777, 0.0368667656031986, 0.0146398923143701, True, 0, Nothing, 0)
            status = swModelDocExt.SelectByID2("Arc1""SKETCHSEGMENT", -0.0802420935887737, 0.0333695230163339, 0.019671897706856, True, 0, Nothing, 0)
            Select Case (lIdx)
                Case 0
                    ' Move
                    bCopy = False
                    lNumCopies = 0
                Case 1
                    ' Move and copy once
                    bCopy = True
                    lNumCopies = 1
                Case 2
                    ' Move and copy twice
                    bCopy = True
                    lNumCopies = 2
            End Select
            If (Not bCopy) Then
                lNumCopies = 0
            End If
            swModelDocExt.MoveOrCopy(bCopy, lNumCopies, True, aBasePoint(0), aBasePoint(1), 0.0#, aBasePoint(0) + aMoveVector(0), aBasePoint(1) + aMoveVector(1), aBasePoint(2) + aMoveVector(2))
            ' Undo so that you can do it again, but differently
        Next lIdx
    End Sub
    ''' <summary>
    ''' The SldWorks swApp variable is pre-assigned for you.
    ''' </summary>
    Public swApp As SldWorks
End Class

Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

Subject:   Feedback on Help Topics
Page:   Move Copy Sketch Entities Example (VB.NET)
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:


We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again

Web Help Content Version: API Help (English only) 2016 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.