Edit iProperties with iLogic

Edit iProperties with iLogic

Anonymous
Not applicable
2,176 Views
14 Replies
Message 1 of 15

Edit iProperties with iLogic

Anonymous
Not applicable

Hi, I'm using this code to edit iproperties of all files inside main folder (oFolder):

 

Sub Main()

Dim oFolder As String
oFolder = ThisDoc.Path

Dim oFiles() As String
oFiles = System.IO.Directory.GetFiles(oFolder)

Dim oFile As String

Dim docFName As String
Dim docFName_wo_ext As String
Dim FNamePos As Long


For Each oFile In oFiles

	FNamePos = InStrRev(oFile, "\", -1)
	docFName = Right(oFile, Len(oFile) - FNamePos)
	docFName_wo_ext = Left(docFName, Len(docFName) - 4)

	Try 
		iProperties.Value(docFName, "Project", "Part Number") = docFName_wo_ext
		'MessageBox.Show(iProperties.Value(docFName, "Project", "Part Number"))
	Catch
		MessageBox.Show("Can't edit iproperties of " + docFName)
	End Try


Next


InventorVb.DocumentUpdate()
MessageBox.Show("Finish!")

End Sub

 

The problem is that the code runs well for all ipts and iams (except the main one) but can't work properly for all idws inside the same folder (oFolder).

Why the code can't edit the main iam file and the idw files?

Can you help me please?

Thanks

 

EDIT: I use Inventor Professional 2018

0 Likes
Accepted solutions (2)
2,177 Views
14 Replies
Replies (14)
Message 2 of 15

tdant
Collaborator
Collaborator

The iLogic iProperties.Value method is only meant for updating components inside and assembly, not the assembly itself or drawings of it. Try this instead:

 

Sub Main()

Dim oFolder As String
oFolder = ThisDoc.Path

Dim oFiles() As String
oFiles = System.IO.Directory.GetFiles(oFolder)

Dim oFile As String

Dim docFName As String
Dim docFName_wo_ext As String
Dim FNamePos As Long
Dim doc As Document
Dim partNum As Property

For Each oFile In oFiles
	FNamePos = InStrRev(oFile, "\", -1)
	docFName = Right(oFile, Len(oFile) - FNamePos)
	docFName_wo_ext = Left(docFName, Len(docFName) - 4
Try Set doc = ThisApplication.Documents.Open(docFName, False)
Catch
Goto NextFor
End Try
Set partNum = doc.PropertySets("Design Tracking Properties").Item("Part Number")
Try partNum.Value = docFName_wo_ext 'MessageBox.Show(partNum.Value) Catch MessageBox.Show("Can't edit iproperties of " + docFName) End Try doc.Close
NextFor: Next InventorVb.DocumentUpdate() MessageBox.Show("Finish!") End Sub
Message 3 of 15

Anonymous
Not applicable

Hi @tdant, thank you for reply, but your code gives me error "The keyword does not indicate a type." at line 15. It seems that "partNum" can't be a Property obj.

 

But really I don't understand why the method "iProperties.Value" doesn't work with IDWs, that are files like IPTs and have own iProperties...

 

Can you investigate the problem? Thank you

0 Likes
Message 4 of 15

tdant
Collaborator
Collaborator

Edited:

 

Sub Main()

Dim oFolder As String
oFolder = ThisDoc.Path

Dim oFiles() As String
oFiles = System.IO.Directory.GetFiles(oFolder)

Dim oFile As String

Dim docFName As String
Dim docFName_wo_ext As String
Dim FNamePos As Long
Dim doc As Document
Dim partNum As Inventor.Property

For Each oFile In oFiles
	FNamePos = InStrRev(oFile, "\", -1)
	docFName = Right(oFile, Len(oFile) - FNamePos)
	docFName_wo_ext = Left(docFName, Len(docFName) - 4)
        Try
             doc = ThisApplication.Documents.Open(docFName, False)
        Catch
             Goto NextFor
        End Try

        partNum = doc.PropertySets("Design Tracking Properties").Item("Part Number")

	Try 
		partNum.Value = docFName_wo_ext
		'MessageBox.Show(partNum.Value)
	Catch
		MessageBox.Show("Can't edit iproperties of " + docFName)
	End Try

        doc.Close
NextFor:
Next


InventorVb.DocumentUpdate()
MessageBox.Show("Finish!")

End Sub

I actually tested this one and it doesn't give any errors.

 

I don't really understand much about the underpinnings of iLogic functions either. It's more difficult to watch iLogic work step by step for a guy like me with a VBA background. It must have something to do with the scope of the function though, because it doesn't even try to address the top-level assembly or any idws, even though they are similar.

0 Likes
Message 5 of 15

Anonymous
Not applicable

Hi @tdant, thank you for your time, I'm sorry but I can't obtain what I want with the code. My target is to edit all "Part Number" in "Project" for each IPTs, subIAMs, IDWs related to a main Assembly. The "Part Number" I want is the filename of the IPT or IAM.

 

I use the Inventor built-in iLogic editor for rules, but your code can't change the "Part Number" of my IDWs (it works with IPTs). I never used VBA editor of Inventor.

 

Are you using a VBA version of the code?

Thank you

 

0 Likes
Message 6 of 15

tdant
Collaborator
Collaborator

I think I made too many assumptions. Let me dig a little deeper.

0 Likes
Message 7 of 15

tdant
Collaborator
Collaborator
Accepted solution

This iLogic rule will read the directory of whatever document runs it, and change the part numbers of any other iam, ipt, or idw files in the same directory to match their relative file names without extensions. This will not affect any files contained in subdirectories.

Sub Main()

Dim oFolder As String
oFolder = ThisDoc.Path

Dim oFiles() As String
oFiles = System.IO.Directory.GetFiles(oFolder)

Dim docFName As String
Dim docFName_wo_ext As String
Dim FNamePos As Long
Dim doc As Document
Dim partNum As Inventor.Property

For Each oFile In oFiles
	FNamePos = InStrRev(oFile, "\", -1)
	docFName = Right(oFile, Len(oFile) - FNamePos)
	docFName_wo_ext = Left(docFName, Len(docFName) - 4)
		Try
             doc = ThisApplication.Documents.Open(oFile, False)
        Catch
             Goto NextFor
        End Try
		
		partNum = doc.PropertySets("Design Tracking Properties").Item("Part Number")
		
		Try 
			partNum.Expression = docFName_wo_ext
		Catch
			MessageBox.Show("Can't edit iproperties of " + docFName)
		End Try
		
        If Not oFile = ThisDoc.PathAndFileName(True) Then
			doc.Close
		End If
NextFor:
Next


InventorVb.DocumentUpdate()
MessageBox.Show("Finish!")

End Sub

This should git'r'done.

Message 8 of 15

Anonymous
Not applicable

Hi @tdant, your last code works very well, thank you! I commented the doc.Close, because I don't want the code closes some opened IDWs.

 

I was curious and I understood from your code that the solution is the use of "oFile" inside "doc = ThisApplication.Documents.Open(oFile, False)" instead of "docFName" at line 20: if I use "doc = ThisApplication.Documents.Open(oFile, False)" the code works, if I use "doc = ThisApplication.Documents.Open(docFName, False)" it doesn't work.

 

A question related to iLogic editor: can I work with Inventor (3D) without closing the iLogic editor? I can't find a way to do it, to modify 3D parts or add some constraints I must close the iLoci editor, that in my opinion is not very practical.

 

Thank you

0 Likes
Message 9 of 15

tdant
Collaborator
Collaborator

The difference between oFile and docFName is that oFile includes the path with the file name. In order to open a file, Inventor needs to know not only the file name, but also where to find it.

 

As far as I know, the iLogic window is always modal, meaning nothing outside of it can be interacted with.

0 Likes
Message 10 of 15

Anonymous
Not applicable

Ok @tdant, thank you for explanation.

About iLogic editor, I can't understand if I can work with Inventor even if the iLogic editor is opened. For iLogic editor I mean :

iLogic Editor.jpg

 

 

and not this:

iLogic Browser.jpg

 

It seems Inventor is blocked when iLogic editor is opened. Can I work with Inventor with the iLogic editor opened?

Thank you

0 Likes
Message 11 of 15

tdant
Collaborator
Collaborator
Accepted solution

Right. The window in the first screenshot is a modal window. Any time it is open, it's the only thing you can interact with. As far as I know, there's no way to change that.

Message 12 of 15

Anonymous
Not applicable

Ok, thank you for all replies! Bye

0 Likes
Message 13 of 15

Anonymous
Not applicable

Hi @tdant, sorry if I return here, but I want to share a different iLogic code that searches for all files inside the folder where there is the main project (.ipj) instead of the main assembly opened (.iam) and in subfolders too. I started to use it with my Pack&Go folders to update the iProperties of a lot of old files. It works well. The code is the following, it is your code with some modifications:

 

Sub Main()

Dim oFolder As String

oFolder = ThisDoc.WorkspacePath()

Dim oFiles() As String
oFiles = System.IO.Directory.GetFiles(oFolder, "*.*", IO.SearchOption.AllDirectories)

Dim oFile As String

Dim docFName As String
Dim docFName_wo_ext As String
Dim FNamePos As Long
Dim doc As Document
Dim partNum As Inventor.Property

For Each oFile In oFiles

	FNamePos = InStrRev(oFile, "\", -1)
	docFName = Right(oFile, Len(oFile) - FNamePos)
	docFName_wo_ext = Left(docFName, Len(docFName) - 4)
	
	Try
		doc = ThisApplication.Documents.Open(oFile, False)
	Catch
		Goto NextFor
	End Try
		
	partNum = doc.PropertySets("Design Tracking Properties").Item("Part Number")
		
	Try 
		partNum.Value = docFName_wo_ext
	Catch
		MessageBox.Show("Can't edit iproperties of " + docFName)
	End Try
	
NextFor:
Next

InventorVb.DocumentUpdate()
MessageBox.Show("Finish!")

End Sub

 

My question: in "oFiles = System.IO.Directory.GetFiles(oFolder, "*.*", IO.SearchOption.AllDirectories)" at the beginning, how can I change "*.*" so that the code searches for files with extensions .ipt, .iam and .idw only? I tried to add "*.ipt OR *.iam OR *.idw", that is the same syntax that I use when I search for files with different extensions in Windows 7, but it doesn't work with Inventor. Do you know if user can search only for some types of files (to increase the speed of the code)?

 

Thank you

 

0 Likes
Message 14 of 15

tdant
Collaborator
Collaborator

Try this:

 

 

Sub Main()

Dim oFolder As String

oFolder = ThisDoc.WorkspacePath()

Dim oFiles() As String
oFiles = System.IO.Directory.GetFiles(oFolder, "*.ipt", IO.SearchOption.AllDirectories)
Array.Copy(System.IO.Directory.GetFiles(oFolder, "*.iam", IO.SearchOption.AllDirectories), 0, oFiles, System.IO.Directory.GetFiles(oFolder, "*.iam", IO.SearchOption.AllDirectories).Length) Array.Copy(System.IO.Directory.GetFiles(oFolder, "*.idw", IO.SearchOption.AllDirectories), 0, oFiles, System.IO.Directory.GetFiles(oFolder, "*.idw", IO.SearchOption.AllDirectories).Length)
Dim oFile As String Dim docFName As String Dim docFName_wo_ext As String Dim FNamePos As Long Dim doc As Document Dim partNum As Inventor.Property For Each oFile In oFiles FNamePos = InStrRev(oFile, "\", -1) docFName = Right(oFile, Len(oFile) - FNamePos) docFName_wo_ext = Left(docFName, Len(docFName) - 4) Try doc = ThisApplication.Documents.Open(oFile, False) Catch Goto NextFor End Try partNum = doc.PropertySets("Design Tracking Properties").Item("Part Number") Try partNum.Value = docFName_wo_ext Catch MessageBox.Show("Can't edit iproperties of " + docFName) End Try NextFor: Next InventorVb.DocumentUpdate() MessageBox.Show("Finish!") End Sub

It isn't refined, but it should make three passes at oFolder, one each for parts, assemblies, and drawings, and append them into the oFiles array. I haven't had time to test it, but maybe that'll get you started if it doesn't work outright.

 

0 Likes
Message 15 of 15

Anonymous
Not applicable

Hi @tdant, thank you for reply, and sorry for delay. Your idea about array is good, now the code for only iam, ipt and idw is the following:

 

Sub Main()

Dim oFolder As String

oFolder = ThisDoc.WorkspacePath()   '' folder of active project
'oFolder = ThisDoc.Path   '' folder of main assembly opened

Dim oFiles() As String
Dim oFile As String
Dim docFName As String
Dim docFName_wo_ext As String
Dim FNamePos As Long
Dim doc As Document
Dim partNum As Inventor.Property
Dim i As Integer
Dim Array_for_extensions() As String = {"*.iam", "*.ipt", "*.idw"}

For i = 0 To 2

	oFiles = System.IO.Directory.GetFiles(oFolder, Array_for_extensions(i), IO.SearchOption.AllDirectories)
	For Each oFile In oFiles
	
		FNamePos = InStrRev(oFile, "\", -1)
		docFName = Right(oFile, Len(oFile) - FNamePos)
		docFName_wo_ext = Left(docFName, Len(docFName) - 4)
		
		Try
			doc = ThisApplication.Documents.Open(oFile, False)
		Catch
			Goto NextFor
		End Try
			
		partNum = doc.PropertySets("Design Tracking Properties").Item("Part Number")
			
		Try 
			partNum.Value = docFName_wo_ext
		Catch
			MessageBox.Show("Can't edit iproperties of " + docFName)
		End Try
		
	NextFor:
	Next
Next


InventorVb.DocumentUpdate()
MessageBox.Show("Finish!")

End Sub

Regards

0 Likes