How to duplicate a sketch for the same plane and same position from the origin sketch using iLogic?

How to duplicate a sketch for the same plane and same position from the origin sketch using iLogic?

moraesorlando
Advocate Advocate
1,246 Views
4 Replies
Message 1 of 5

How to duplicate a sketch for the same plane and same position from the origin sketch using iLogic?

moraesorlando
Advocate
Advocate

Hello all Inventor experts!

 

            I am here again looking for help.

 

            At this time, I have an .ipt file and I am trying to duplicate a sketch in the tree using iLogic.

 

            In my current scenario I have a sketch named: “Sketch – Default” and I would like to make a copy of this sketch and past it in the same plane and same position than the previous sketch and to rename it to “Sketch – In work”.

 

            My target is to get a sketch exactly the same than the Default sketch (including to be fully constrained), with the only different in the name, that need to be “Sketch – In work”

 

            My idea regarding how this can works:

 

  1. The “Sketch – In work” is deleted
  2. The “Sketch – Default” is copied
  3. The “Sketch – Default” is pasted
  4. The “Sketch – Default” is renamed for “Sketch in work”
  5. End

 

            Please see the image attached regarding my current situation.

 

           

 

            From my previous researches regarding this matter in this forum and in the web I was could find the post below, but is not exactly what I need.

 

            How to copy and paste sketch in same plane - Autodesk Community - Inventor

 

            As always, I am available to provide any other additional information required.

 

            Thanks in advance,

0 Likes
Accepted solutions (1)
1,247 Views
4 Replies
Replies (4)
Message 2 of 5

Ralf_Krieg
Advisor
Advisor

Hello

 

Try this one

If Not ThisDoc.Document.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
	MsgBox("Function only in part documents available.", MsgBoxStyle.Critical, "iLogic Copy Sketch")
	Exit Sub
End If

Dim oDoc As PartDocument = ThisDoc.Document
Dim oTrans As Transaction = ThisApplication.TransactionManager.StartTransaction(oDoc, "Copy Sketch")

Try
	Dim oSketch As PlanarSketch
	For Each oSketch In oDoc.ComponentDefinition.Sketches
	    If oSketch.Name = "Sketch - In work" Then
	        If oSketch.Consumed = True Then
	            MsgBox ("Sketch 'Sketch - In work' is is use. Abort function",MsgBoxStyle.Critical,"iLogic Copy Sketch")
	            Exit Sub
	        End If
	        oSketch.Delete
	        Exit For
	    End If
	Next
	
	For Each oSketch In oDoc.ComponentDefinition.Sketches
	    If oSketch.Name = "Sketch - Default" Then Exit For
	Next
	
	If oSketch Is Nothing Then
	    MsgBox ("Sketch 'Sketch - Default' not found. Abort function",MsgBoxStyle.Critical,"iLogic Copy Sketch")
	    Exit Sub
	End If
	
	Dim oNewSketch As PlanarSketch
	If oSketch.AxisEntity Is Nothing Then
	    oNewSketch = oDoc.ComponentDefinition.Sketches.Add(oSketch.PlanarEntity)
	Else
		Dim oPoint As Object
		If oSketch.OriginPoint Is Nothing Then
			oPoint=oDoc.ComponentDefinition.WorkPoints(1)
		Else
			oPoint=oSketch.OriginPoint
		End If
	    oNewSketch = oDoc.ComponentDefinition.Sketches.AddWithOrientation(oSketch.PlanarEntity, oSketch.AxisEntity, oSketch.NaturalAxisDirection, oSketch.AxisIsX, oPoint)
	End If
	
	oSketch.CopyContentsTo(oNewSketch)
	
	oNewSketch.Name = "Sketch - In work"
	
	oTrans.End
Catch ex As Exception
	MsgBox(ex.Message, MsgBoxStyle.Critical, "iLogic Copy Sketch")
	oTrans.Abort
End Try

 

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
Message 3 of 5

moraesorlando
Advocate
Advocate

Hello @Ralf_Krieg !

First of all thanks for your reply.

 

I have spent the last few days testing the code you have suggested in a simplified simulation of my real situation.

 

I could see that the sketch is copied, pasted and renamed, thanks a lot for that.

 

This shows we are in the correct path but some adjustments are required to reach the target.

 

Please take a look in the simulation I have prepared.

 

I have identified two initial points to received attention:

 

01 – The target was to maintain the new sketch pasted fully constrain as the original but this is not occurring. The new sketch has the same lines but is not fully constraint;

        The “Sketch – Default” has a constraint associated with the center point. But this constraint is not been kept in the new “Sketch - In work”

 

02 – The “Sketch – Default” has a dimension associated with the width parameter and its value 15 mm.

        After the “Sketch – Default” have been copied and become the “Sketch - In work”, this dimension lost the connection with the parameter and become a dimension with values not with parameters.

        The target is to maintain the dimensions associate to the parameter also in the “Sketch - In work”

 

Once again, thanks in advance and feel free to ask any questions or requests in order to continue this matter.

 

Best regards

0 Likes
Message 4 of 5

Ralf_Krieg
Advisor
Advisor
Accepted solution

Hello

 

I think the following code can preserve the geometric constrains. The parameter linking is lost while deleting the old "Sketch In work". But we can try to copy the parameter expressions from the "Sketch Default" to the new "Sketch In work".

 

If Not ThisDoc.Document.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
	MsgBox("Function only in part documents available.", MsgBoxStyle.Critical, "iLogic Copy Sketch")
	Exit Sub
End If

Dim oPartDoc As PartDocument = ThisDoc.Document
Dim oCompDef As PartComponentDefinition = opartDoc.ComponentDefinition
Dim oTrans As Transaction = ThisApplication.TransactionManager.StartTransaction(opartDoc, "Copy Sketch")

Try
	Dim oSourcesketch As PlanarSketch
	For Each oSourcesketch In opartDoc.ComponentDefinition.Sketches
	    If oSourcesketch.Name = "Sketch - In work" Then
	        If oSourcesketch.Consumed = True Then
	            MsgBox ("Sketch 'Sketch - In work' is is use. Abort function",MsgBoxStyle.Critical,"iLogic Copy Sketch")
	            Exit Sub
	        End If
	        oSourcesketch.Delete
	        Exit For
	    End If
	Next
	
	For Each osourceSketch In oCompDef.Sketches
	    If oSourcesketch.Name = "Sketch - Default" Then Exit For
	Next
	
	If oSourcesketch Is Nothing Then
	    MsgBox ("Sketch 'Sketch - Default' not found. Abort function",MsgBoxStyle.Critical,"iLogic Copy Sketch")
	    Exit Sub
	End If

	opartDoc.SelectSet.Clear
	opartDoc.SelectSet.Select(oSourcesketch)
	
	Dim oWP As Object=oSourcesketch.PlanarEntity 'WorkPlane= oCompDef.WorkPlanes(3)
	
	Dim oCopyControlDef As ControlDefinition= ThisApplication.CommandManager.ControlDefinitions.Item("AppCopyCmd")
	Dim oPasteControlDef As ControlDefinition= ThisApplication.CommandManager.ControlDefinitions.Item("AppPasteCmd")
	
	oCopyControlDef.Execute
	opartDoc.SelectSet.Clear
	opartDoc.SelectSet.Select(oWP)
	oPasteControlDef.Execute
	
	Dim oTargetSketch As Sketch = oCompDef.Sketches.Item(oCompDef.Sketches.Count)
	
	Dim i As Integer
	Dim oDim As DimensionConstraint
	For i = 1 To oSourcesketch.DimensionConstraints.Count
	    If oTargetSketch.DimensionConstraints.Item(i).Parameter.Type = kModelParameterObject Then
	        oTargetSketch.DimensionConstraints.Item(i).Parameter.Expression = oSourcesketch.DimensionConstraints.Item(i).Parameter.Expression
	    End If
	Next

	oTargetSketch.Name = "Sketch - In work"
	
	oTrans.End
Catch ex As Exception
	MsgBox(ex.Message, MsgBoxStyle.Critical, "iLogic Copy Sketch")
	oTrans.Abort
End Try

R. Krieg
RKW Solutions
www.rkw-solutions.com
Message 5 of 5

moraesorlando
Advocate
Advocate

Hello @Ralf_Krieg !!!

 

Yesterday I transferred this code to my my schematic situation and tested. The result is:
It works correctly.

 

Then I have transferred this code for my real situation and the result is:

Success!!!

 

One more time, thank you very much for your support.

 

I will mark this post as resolved.

 

I will continue posting interesting topics.

 

I hope to see you again in my next Inventor pursuits.

 

Thanks again and best regards,

 

0 Likes