Copy sketch from different file to active file

Copy sketch from different file to active file

dimamazutaMMJ32
Advocate Advocate
589 Views
7 Replies
Message 1 of 8

Copy sketch from different file to active file

dimamazutaMMJ32
Advocate
Advocate

Hello!

I am looking for the way to copy a sketch from another file to the created sketch of the active file, or to select a working plane to copy the sketch.

Thank you for your help.

0 Likes
Accepted solutions (1)
590 Views
7 Replies
Replies (7)
Message 2 of 8

A.Acheson
Mentor
Mentor

Are you using vba or ilogic? 

Here is the api sample for copying a sketch  in the same file. You will need to create a reference to your source file definition. If that file is open then this snippet will set its reference. 

Dim oDoc As Document  

Set oDoc =ThisApplication.Documents.ItemByName( _
          "C:\AssemblySample\Pillar.ipt")

 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 8

dimamazutaMMJ32
Advocate
Advocate

Usually I use ilogic. Unfortunately, my programming knowledge is very basic and I don't know the methods to do the copying. What should I do after reference to the file I want to copy the sketch from?

0 Likes
Message 4 of 8

A.Acheson
Mentor
Mentor

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
0 Likes
Message 5 of 8

dimamazutaMMJ32
Advocate
Advocate

Thank you for your detailed answer.

I've created some "Frankenstein's Monster" that consists of different pieces of code and I can`t place sketch on plane what I piked, only on WorkPlanes.Item(1) 

 

filename="F:\Work\DesingsHome\iLogic Rules\BOM\01.01.01-02 Деталь.ipt"
PartDoc=ThisApplication.Documents.Open(filename,False)
PartcompDef=PartDoc.ComponentDefinition
ExtFeature=PartcompDef.Features.ExtrudeFeatures.item(1)
Dim ExtDef As ExtrudeDefinition = ExtFeature.Definition
ExtProfile=ExtDef.Profile

Dim osketch As PlanarSketch
oSketchToCopy=ExtFeature.Definition.Profile.Parent

ThisDoc.Document.Activate
Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument
Dim oPlane As WorkPlane  '-my additional WorkPlane for sketch
oPlane = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kWorkPlaneFilter, "") '-pick the WorkPlane
 
Dim Sketch2 As PlanarSketch
Sketch2 = oDoc.ComponentDefinition.Sketches.Add(oDoc.ComponentDefinition.WorkPlanes.Item(1),False)
'Sketch2 = oDoc.ComponentDefinition.Sketches.Add(oDoc.ComponentDefinition.oPlane) '-here I tried to place sketch in WorkPlane but I didn't find the wright way to do it
oSketchToCopy.CopyContentsTo(Sketch2)




 

0 Likes
Message 6 of 8

A.Acheson
Mentor
Mentor
Accepted solution

You were almost there, you just needed to place the workplane object you just selected into the Sketches.Add method.

 

Just swap "oPlane" for the first plane "oDoc.ComponentDefinition.WorkPlanes.Item(1)"

Dim Sketch2 As PlanarSketch = oDoc.ComponentDefinition.Sketches.Add(oPlane,False)

Here is the rest of the rule cleaned up with declarations. Always best practice to declare the objects to gain access to there methods.

 

Dim filename As String = "F:\Work\DesingsHome\iLogic Rules\BOM\01.01.01-02 Деталь.ipt"
Dim PartDoc As PartDocument = ThisApplication.Documents.Open(filename,False)
Dim PartcompDef As PartComponentDefinition = PartDoc.ComponentDefinition

Dim ExtFeature As ExtrudeFeature = PartcompDef.Features.ExtrudeFeatures.item(1)
Dim ExtDef As ExtrudeDefinition = ExtFeature.Definition
Dim ExtProfile As Profile = ExtDef.Profile

Dim oSketchToCopy As PlanarSketch = ExtFeature.Definition.Profile.Parent

Dim oDoc As PartDocument = ThisDoc.Document

Dim oPlane As WorkPlane = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kWorkPlaneFilter, "") '-pick the WorkPlane
 
Dim Sketch2 As PlanarSketch = oDoc.ComponentDefinition.Sketches.Add(oPlane,False)'oDoc.ComponentDefinition.WorkPlanes.Item(1)

oSketchToCopy.CopyContentsTo(Sketch2)

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 7 of 8

dimamazutaMMJ32
Advocate
Advocate

Thank you, this is exactly what I need!

Message 8 of 8

dimamazutaMMJ32
Advocate
Advocate

One more question. Where can I find information about objects and their methods? There are many videos about ilogic in Youtube, but they They show only basic snippets of ilogic or very difficult code for a beginner with different methods and other things, witch I don't even know names of. Maybe you know some course or literature?

0 Likes