Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
A.Acheson
in reply to: dimamazutaMMJ32

Here is a simple version of how to copy a sketch using the method CopyContentsTo. Pay attention to the objects that change between the source and target. These are Document, ComponentDefinition and Sketch. These object names have been renamed to make it easier to track what is happening. 

Option1:

'a reference to the Source document.
	 Dim oSourceDoc As PartDocument
     oSourceDoc = ThisApplication.Documents.ItemByName("C:\Users\1234-M-001.ipt")
 
    'a reference to the component definition.
    Dim oSourceDef As PartComponentDefinition
     oSourceDef = oSourceDoc.ComponentDefinition
    
    'a reference to the first sketch in the part.
    Dim oSketchToCopy As PlanarSketch
     oSketchToCopy = oSourceDef.Sketches.Item(1)
 

    'a reference to the the Target document you launch the rule from.
    Dim oTargetDoc As PartDocument
     oTargetDoc = ThisDoc.Document
	
	'a reference to the component definition.
    Dim oTargetDef As PartComponentDefinition
     oTargetDef  = oTargetDoc.ComponentDefinition
	 
    'Create a new sketch on the XY plane.
    Dim oNewSketch As PlanarSketch
     oNewSketch = oTargetDef.Sketches.Add(oTargetDef.WorkPlanes.Item(3))
    
    'Copy the sketch from source to target.
      oSketchToCopy.CopyContentsTo(oNewSketch)
    
  

There is an optional piece of code to move the sketch once pasted. It gathers each piece of the sketch into a collection and moves it to a new location. 

'	'Optional move sketch to new location
'    Dim oSketchEnts As ObjectCollection
'     oSketchEnts = ThisApplication.TransientObjects.CreateObjectCollection
    
'    Dim oSketchEnt As SketchEntity
'    For Each oSketchEnt In oNewSketch.SketchEntities
'        Call oSketchEnts.Add(oSketchEnt)
'    Next
    
'    ' Translate all sketch entities in the new sketch.
'   oNewSketch.MoveSketchObjects(oSketchEnts, ThisApplication.TransientGeometry.CreateVector2d(10, 0))

Option2:

Using the API sample was more difficult than expected. The reason being it just mimicks the manual operation by using the command manager which is the same as hitting a button on the user interface. To achieve a result it is necessary to  activate the source document to correctly pick up the sketch. Here is the modifications required to copy from one source to another. 

Disable the screen update when documents are being activated
	ThisApplication.ScreenUpdating = False
  	
	'a reference to the Source document.
	 Dim oSourceDoc As PartDocument
     oSourceDoc = ThisApplication.Documents.ItemByName("C:\Users\1234-M-001.ipt")
  	 oSourceDoc.Activate
    
	'a reference to the component definition.
    Dim oSourceDef As PartComponentDefinition
     oSourceDef = oSourceDoc.ComponentDefinition
    
    'a reference to the first sketch in the part.
    Dim oSketchToCopy As PlanarSketch
     oSketchToCopy = oSourceDef.Sketches.Item(1)
   
    'Select the sketch to copy.
    oSourceDoc.SelectSet.Clear
    oSourceDoc.SelectSet.Select(oSketchToCopy)

    'Execute the copy command.
    Dim oCopyControlDef As ControlDefinition
     oCopyControlDef = ThisApplication.CommandManager.ControlDefinitions.Item("AppCopyCmd")
     oCopyControlDef.Execute
  	
    'a reference to the active document.
    'This assumes a part document is active.
    Dim oDoc As PartDocument
     oDoc = ThisDoc.Document
	 oDoc.Activate
	'a reference to the component definition.
    Dim oDef As PartComponentDefinition
     oDef = oDoc.ComponentDefinition
	 
    'Create a new sketch on the XY plane.
    Dim oNewSketch As PlanarSketch
     oNewSketch = oDef.Sketches.Add(oDef.WorkPlanes.Item(3))
    
    'Put the sketch in edit mode.
    oNewSketch.Edit
	
    'Execute the paste command.
    Dim oPasteControlDef As ControlDefinition
     oPasteControlDef = ThisApplication.CommandManager.ControlDefinitions.Item("AppPasteCmd")
     oPasteControlDef.Execute

	ThisApplication.ScreenUpdating = True

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan