I'm trying to write some code that will allow me to copy a sketch from one .ipt file and then paste it into a new .ipt file where I can then do some manipulations and eventually export it elsewhere (the manipulations and export are irrelevant here). I've got some code for copy and paste but I am having a terrible time with getting inventor to create a new part (IPT) where I can the place my copied sketch. I've also had no luck trying the "make part" command with iLogic. Any ideas on what would make this work? I've attached a few screenshots to give an idea of what I'm doing.
Solved! Go to Solution.
Solved by WCrihfield. Go to Solution.
Hi @rickzacher. I don't know how you planned on getting the source sketch object, so I just assumed that the 'Pick' method would be OK to start with. I also did not know which origin plane in the new part you would want to paste the sketch to, so I just chose the first one, because that is simple. With that in mind, here is an iLogic example of one way to do it that worked for me.
Dim oSourceSketch As Sketch = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kSketchObjectFilter, "Select a Sketch to copy.") If oSourceSketch Is Nothing OrElse (Not TypeOf oSourceSketch Is Sketch) Then MsgBox("Either nothing, or wrong type selected. Exiting.", , "") Exit Sub End If Dim oSourcePDef As PartComponentDefinition = oSourceSketch.Parent Dim oSourcePDoc As PartDocument = oSourcePDef.Document oSourcePDoc.SelectSet.Clear oSourcePDoc.SelectSet.Select(oSourceSketch) ThisApplication.CommandManager.ControlDefinitions.Item("AppCopyCmd").Execute Dim oPDoc As PartDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject, , True) oPDef = oPDoc.ComponentDefinition oSketches = oPDef.Sketches oPlane = oPDef.WorkPlanes.Item(1) oSketch = oSketches.Add(oPlane) oSketch.Edit ThisApplication.CommandManager.ControlDefinitions.Item("AppPasteCmd").Execute
If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.
If you want and have time, I would appreciate your Vote(s) for My IDEAS :light_bulb: or you can Explore My CONTRIBUTIONS
Wesley Crihfield
(Not an Autodesk Employee)
@WCrihfield Thanks for the response. I guess I should have included the code that I have so far in my initial question. I've already created a named sketch that is specific to the operation I'm performing so I don't think I need to go with the "pick" method. All I simply need to do is copy the specific named sketch. The part that I'm getting stuck on is getting Inventor to effectively do the mouse clicks for "File>New>Part". Once I have that piece of the puzzle I am fairly confident I can execute the copy / paste / save as commands. My only other thought would be to take my named sketch ("Tool_Profile") and run the "make part" command.
I don't know which one of the two options would be best/easiest to accomplish.
Here is my current code. Plane selection shouldn't be an issue once I get to that point.
Feature.IsActive("male_thread_coil") = False
Dim oDoc As PartDocument = ThisDoc.Document
' Set a reference to the component definition.
Dim oCD As PartComponentDefinition
oCD = oDoc.ComponentDefinition
'Select Plane
'Dim oPlaneYZ As WorkPlane = oCD.WorkPlanes.Item(1)
'Dim oPlaneXZ As WorkPlane = oCD.WorkPlanes.Item(2)
'Dim oPlaneYZ As WorkPlane = oCD.WorkPlanes.Item(3)
Dim oPlaneYZ As WorkPlane = oCD.WorkPlanes.Item(1)
' Create a new sketch.
Dim oSketch As Sketch
oSketch = oCD.Sketches.Add(oPlaneYZ)
oSketch.Name = "Tool_Profile"
oSketch.edit
'project all cut edges
Dim oTransGeom As TransientGeometry
oTransGeom = ThisApplication.TransientGeometry
Dim oDef As ControlDefinition
oDef = ThisApplication.CommandManager.ControlDefinitions.Item("SketchProjectCutEdgesCmd")
oDef.Execute
oSketch.ExitEdit
Hi @rickzacher. I'm heading out for the day/weekend, but other than the 'make part' command, most of the pieces of the puzzle are within the code I posted. I'm showing how to add the sketch object to the SelectSet of the document, which simulates you clicking on it to select it. Then how to execute the command that simulates 'Copy' to clipboard. Then I'm showing how to create a new Part document. You can specify what Template part to use as the second input variable in that line if you want. I just left that part out because it is optional, and was not not critical for my example. I am then also showing you what you would need to do to paste that copied sketch into the newly created part. You first have to create a sketch, then put it into Edit mode, then execute the Paste command. Since you already have variables for the source part document and the sketch, you don't need to use the Pick method, but you can probably scavenge some other useful stuff from that.
Have a great weekend. 🙂
Wesley Crihfield
(Not an Autodesk Employee)
Thanks so much for your help. That worked like a charm. I did go ahead and removed the "pick" function and went with my named sketch. It works so well now. Hope you have/had a great weekend too. Cheers!
Can't find what you're looking for? Ask the community or share your knowledge.