HELP! Copy a part using iLogic

HELP! Copy a part using iLogic

mtresky
Contributor Contributor
1,710 Views
8 Replies
Message 1 of 9

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
Accepted solutions (1)
1,711 Views
8 Replies
Replies (8)
Message 2 of 9

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

 

Message 3 of 9

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
Message 4 of 9

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)

Message 5 of 9

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.

Message 6 of 9

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
Message 7 of 9

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
Message 8 of 9

EBlake7HCRA
Observer
Observer

What if the part isn't in the assembly yet? I have a library of "templates", I would like to copy a part from the library into a folder with the current assembly, and save as it's own part. When I try your code, since the past isn't in this folder to begin with, it doesn't work. I tried modifying the code to change the source file location, but am not sure how to change the ComponentOccurence line to do this.

0 Likes
Message 9 of 9

WCrihfield
Mentor
Mentor

Hi @EBlake7HCRA.  If you just want to copy a file from one place to another, then there are multiple ways to do that pretty easily.  I will show two of those ways below:

System.IO.File.Copy Method 

 

Dim sTemplatePart As String = "C:\Temp\My Template Part 1.ipt"
Dim sFileName As String = System.IO.Path.GetFileName(sTemplatePart)
Dim sNewPart As String = System.IO.Path.Combine(ThisDoc.Path & sFileName)
System.IO.File.Copy(sTemplatePart, sNewPart, True) 'True = Overwrite existing

 

FileManager.CopyFile Method 

 

ThisApplication.FileManager.CopyFile(sTemplatePart, sNewPart, FileManagementEnum.kOverwriteExistingFile)

 

But keep in mind...you may need to name the resulting file differently than the original file, if you are using Vault, or have your Project File settings set to not allow duplicate file names within your project workspace.  The code above is just a quickie example of some code resources that can be used to copy a file, and it assumes that the template file is in a different folder than the 'current' document.  It also assumes that the current document has already been saved, otherwise the current document will not have a File associated with it, and therefore no path or file name that the code can use.  It is just using the template files name for the new file, in a different folder, which may not always work, as mentioned.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes