Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

HELP! Copy a part using iLogic

mtresky
Contributor

HELP! Copy a part using iLogic

mtresky
Contributor
Contributor

I have read through numerous forums on copying a part, none of which seem to help me here. 

I have an assemly template, in this template I have a single part I wish to copy as a new file and save in a specified folder location.

 

Example, I have "Conduit1.ipt" as a component in an assembly, I'm looking for a code to grab the existing part, copy it as "Conduit2.ipt" and save it in the folder the assembly is saved in.

 

Please help me find a code I can do a simple copy and rename of the file name! I'm going crazy! 

0 Likes
Reply
Accepted solutions (1)
1,314 Views
6 Replies
Replies (6)

Anonymous
Not applicable

I think something like this should work for you.

 

Dim oApp as Inventor.Application = ThisApplication
Dim oCopyDoc as Document

Dim oCopyPartName as String = "New part name here"
Dim oAssyPath as String = ThisDoc.Path
Dim oCopyPath as String = oAssyPath & "\" & oCopyPartName & ".ipt"

Dim oPart as ComponentOccurrence = Components("Part to copy name here")
Dim oPartDoc as PartDocument = oPart.Definition.Document
Dim oPartFile as String = oPartDoc.FullFileName

oCopyDoc = oApp.Documents.Open(oPartFile,False)
oCopyDoc.SaveAs(oCopyPath,True)
oCopyDoc.Close

 

mtresky
Contributor
Contributor

@Anonymous  Thank you for the response.

 

Is there any way to accomplish this without having to open, save, close each time? Possibly just specify the part to copy, a new name, and have it copy?

 

I only ask because this method works, however, in an assembly with a ton of parts it takes quite a while to run through when you have 50 parts to copy.

 

Thanks

0 Likes

WCrihfield
Mentor
Mentor

Try this iLogic rule.

Since your file name always has an Integer type number at the end of it, and that Integer may be 2 or 3 digits long, I created a Function to extract that number, then add 1 to it, to use in the new file name.

The component is already considered 'open' because it is in the active assembly, so it doesn't need to be opened again.

If within the SaveAs Sub, you specify true, that means it is doing a SaveCopyAs, so the original document remains open, while the new document is saved off, so you shouldn't need to close anything either.

Sub Main
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("This rule '" & iLogicVb.RuleName & "' only works for Assembly Documents.",vbOK, "WRONG DOCUMENT TYPE")
	Return
End If

Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oOcc As ComponentOccurrence = oADef.Occurrences.Item(1)
Dim oOccDoc As PartDocument = oOcc.Definition.Document
Dim oPath As String = Left(oOccDoc.FullFileName, InStrRev(oOccDoc.FullFileName, "\"))
Dim oOldName As String = IO.Path.GetFileNameWithoutExtension(oOccDoc.FullFileName)
Dim oInt As Integer = ExtractInteger(oOldName)
Dim oName As String = Replace(oOldName, oInt, Nothing)
Dim oNewName As String = oName & oInt + 1 & ".ipt"
oOccDoc.SaveAs(oNewName,True)
End Sub
Function ExtractInteger(oSt As String) As Integer
	Dim oNumSt As String
	For i = 1 To Len(oSt)
		Select Case Mid(oSt, i)
		Case "1"
			oNumSt = oNumSt & Mid(oSt, i)
		Case "2"
			oNumSt = oNumSt & Mid(oSt, i)
		Case "3"
			oNumSt = oNumSt & Mid(oSt, i)
		Case "4"
			oNumSt = oNumSt & Mid(oSt, i)
		Case "5"
			oNumSt = oNumSt & Mid(oSt, i)
		Case "6"
			oNumSt = oNumSt & Mid(oSt, i)
		Case "7"
			oNumSt = oNumSt & Mid(oSt, i)
		Case "8"
			oNumSt = oNumSt & Mid(oSt, i)
		Case "9"
			oNumSt = oNumSt & Mid(oSt, i)
		Case "0"
			oNumSt = oNumSt & Mid(oSt, i)
		End Select
	Next
	Return CInt(oNumSt)
End Function

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Anonymous
Not applicable
Accepted solution

As per @WCrihfield advice, since your assembly is open you should be able to remove both the Open and Close methods from my code and it should still work. I used open/close when copying from outside an assembly and assumed it worked the same.

 

Dim oApp as Inventor.Application = ThisApplication

Dim oCopyPartName as String = "New part name here"
Dim oAssyPath as String = ThisDoc.Path
Dim oCopyPath as String = oAssyPath & "\" & oCopyPartName & ".ipt"

Dim oPart as ComponentOccurrence = Components("Part to copy name here")
Dim oPartDoc as PartDocument = oPart.Definition.Document

oPartDoc.SaveAs(oCopyPath,True)

 

For naming/copying a lot of parts, if you want to iterate through a bunch of parts and name/copy them automatically that will be quite a bit more code. If you're looking for a more manual way, I'd make a simple form in Inventor with two text User Parameters inputs and a button to run the rule to copy.

WCrihfield
Mentor
Mentor

There is always the "iLogic Design Copy" tool too, if you're familiar with that.

To access it, close all other Inventor documents, but leave Inventor open.

Then it is on the Tools tab / iLogic panel.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

mtresky
Contributor
Contributor

Thanks for your help, I got it to work using the following loop:

 

Private Sub CopyPipes()

Dim oPart = Component.InventorComponent("CON1:1")
Dim oPartDoc As PartDocument = oPart.Definition.Document
Dim oPath As String = ThisDoc.Path


	For XX As Double = 1 To (COUNT1 + COUNT2 + COUNT3 + COUNT4 + COUNT5) Step 1		
		
		oPartDoc.SaveAs(oPath & "/CON" & XX & ".ipt", True)

	Next

End Sub		

 I was able to mimic it for my straps and strut as well:

 

Private Sub CopyStraps()

Dim oPart = Component.InventorComponent("Strut Strap1:1")
Dim oPartDoc As PartDocument = oPart.Definition.Document
Dim oPath As String = ThisDoc.Path


	For XX As Double = 1 To (COUNT1 + COUNT2 + COUNT3 + COUNT4 + COUNT5)*2 Step 1		
		
		oPartDoc.SaveAs(oPath & "/Strut Strap" & XX & ".ipt", True)

	Next

End Sub		
0 Likes