Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Run ilogic rule in each part from the assembly via external rule?

20 REPLIES 20
SOLVED
Reply
Message 1 of 21
guyh
7246 Views, 20 Replies

Run ilogic rule in each part from the assembly via external rule?

Hi,

 

I want to simply set a custom iproperty as the Description in each part file within my assembly.  I can do this easily at part level with ilogic but I want to be able to run the rule from the assembly level and all the parts run the rule.  I am struggling to find a way to do this.

 

I think this may be the best method I have found but I think it must be far easier?

 

http://inventortrenches.blogspot.com/search/label/Autodesk%20Inventor%20iLogic

 

Thanks,

Guy

AutoCADM 2011 SP2
Inventor Pro 2011 SP2
20 REPLIES 20
Message 2 of 21
MegaJerk
in reply to: guyh

Will the description be something that is derived from a parameter within each part, a manual entry, or perhaps a parameter in the assembly, OR something just hardcoded? 



If my solution worked or helped you out, please don't forget to hit the kudos button 🙂
iLogicCode Injector: goo.gl/uTT1IB

GitHub
Message 3 of 21
guyh
in reply to: MegaJerk

We have a PLM system which populates a custom iproperty with the description entered in the PLM.  We also have some old parts which have the description in the Description field but not the custom iproperty.  I want to write a rule which will ensure I can call the Description field in a parts list.  If I do this currently some descriptions are blank.

 

Can you help?

 

Thanks,

Guy

AutoCADM 2011 SP2
Inventor Pro 2011 SP2
Message 4 of 21
MegaJerk
in reply to: guyh

So your PLM system does a lot of the grunt work of populating the custom iprop. You just want to make sure that the custom iprop exists, correct?  



If my solution worked or helped you out, please don't forget to hit the kudos button 🙂
iLogicCode Injector: goo.gl/uTT1IB

GitHub
Message 5 of 21
guyh
in reply to: MegaJerk

I want to make sure that if the custom iprop exists it is copied to the Description field under the project tab.  I think it's dangerous to copy the other way as this is data placed in the part from the PLM system.   That way I can run the rule in the assembly and ensure I have the description for each part in my Parts List.  Some of the legacy parts in the PLM had the description stored in the Description field.

 

I do not know why it was changed......

 

cheers,

Guy

AutoCADM 2011 SP2
Inventor Pro 2011 SP2
Message 6 of 21
MegaJerk
in reply to: guyh

Alright. I made this during lunch just now, and have not tested it, but you should be able to throw this into a rule in the main assembly file and let it do its thing. 

Basically it just goes through all of the parts in the assembly, and will set the Project's - Description iproperty, to match a custom iproperty of your desire. Just be sure to replace the custom iproperty name in the code with the one you'll be using. 

Dim openDoc As Document
openDoc = ThisDoc.Document

Dim docFile As Document

If openDoc.DocumentType = 12291 Then

	For Each docFile In openDoc.AllReferencedDocuments
		
		If docFile.DocumentType = 12290 Then
			
			iProperties.Value(docFile.DisplayName, "Project", "Description") = iProperties.Value(docFile.DisplayName, "Custom", "YOURiPropNAMEHERE")
		
		End If 
	
	Next
	
End If 
			
			

 

 



If my solution worked or helped you out, please don't forget to hit the kudos button 🙂
iLogicCode Injector: goo.gl/uTT1IB

GitHub
Message 7 of 21
guyh
in reply to: MegaJerk

Hi Sorry for the delay replying.  Start of the day in the UK now.

 

Thanks a lot for the help and the rule.  I tried it and receive an error when it's run.

 

I've used a test assembly.  Here is the detail of the message

 

 

Cheers,

Guy

AutoCADM 2011 SP2
Inventor Pro 2011 SP2
Message 8 of 21
MegaJerk
in reply to: guyh

I just tested this. It should work, but it assumes that you have your occurrence names set to the filename, and that there is always one of them named with the suffix of “:1” (So if you have a filename called FrankyLovesCake.ipt it assumes the Occurrence name is FrankyLovesCake:1).

Later on, I’ll try to make this more solid and less awful.

 

New Code : 

 


Dim openDoc As Document
openDoc = ThisDoc.Document

Dim docFile As Document

If openDoc.DocumentType = 12291 Then

For Each docFile In openDoc.AllReferencedDocuments

If docFile.DocumentType = 12290 Then

Dim OccName As String
OccName = Left(docFile.DisplayName, Len(docFile.DisplayName) - 4) & ":1"
iProperties.Value(OccName, "Project", "Description") = iProperties.Value(OccName, "Custom", "CUSTOM_NAME_HERE")

End If

Next

End If




If my solution worked or helped you out, please don't forget to hit the kudos button 🙂
iLogicCode Injector: goo.gl/uTT1IB

GitHub
Message 9 of 21
guyh
in reply to: MegaJerk

Hi,

 

Thanks I really appreciate the help.  The file name and occurrence names differ unfortunately.  As an example.  The file name = doc-0000837756_0.ipt yet the occurrence name = DOC-0000837756-PART_GLFT_0003-PART-ID-2301831:1

 

This is generated by the software we have which links to the PLM.

 

Cheers,

Guy

AutoCADM 2011 SP2
Inventor Pro 2011 SP2
Message 10 of 21
MegaJerk
in reply to: guyh

Yeah. I figured that might be the case. Once I get some time, I'll change how it goes about messing with things, and maybe all will be well in the end!. 
 



If my solution worked or helped you out, please don't forget to hit the kudos button 🙂
iLogicCode Injector: goo.gl/uTT1IB

GitHub
Message 11 of 21
MegaJerk
in reply to: guyh

Alright. This will go through every part and change the Description to the Custom Iprop without all the nasty errors or messes. 

 

Just make a new rule in your assembly and give it a go. 

 

Dim openDoc As Document
openDoc = ThisDoc.Document

Dim docFile As Document

If openDoc.DocumentType = 12291 Then

	For Each docFile In openDoc.AllReferencedDocuments

		If docFile.DocumentType = 12290 Then

			Dim assemblyDoc As AssemblyDocument
			assemblyDoc = openDoc

			Dim assemblyDef As AssemblyComponentDefinition
			assemblyDef = assemblyDoc.ComponentDefinition

			Dim partDoc As PartDocument
			partDoc = ThisApplication.Documents.Open(docFile.FullFileName, False)
			
			iProperties.Value(docFile.DisplayName, "Project", "Description") = iProperties.Value(docFile.DisplayName, "Custom", "CUSTOM_NAME_HERE")
			Else 
			'''iProperties.Value(docFile.DisplayName, "Project", "Description") = "Assembly" 
			'''Don't really know if you want anything to happen if there is a Sub Assembly that this code comes across. 
			'''If not, you could just take this entire else portion out. 
		End If 
		
	Next
	
	Else
	
	MessageBox.Show("You must have a valid Assembly document open before using this code!", "File Type Mismatch!",MessageBoxButtons.OK,MessageBoxIcon.Exclamation)
	
End If 

 

 

 

 



If my solution worked or helped you out, please don't forget to hit the kudos button 🙂
iLogicCode Injector: goo.gl/uTT1IB

GitHub
Message 12 of 21
guyh
in reply to: MegaJerk

Hi,

 

Thanks again.  I tried this on the same assembly and it failed.  I made a new assembly and this also failed.  It failed on the same issue as before.  I have attached the new simple 2 part assembly.  These parts have the description in the custom property so the rule should copy this to the project description.

 

Maybe you can see some issue with these files?

 

cheers,

Guy

 

AutoCADM 2011 SP2
Inventor Pro 2011 SP2
Message 13 of 21
MegaJerk
in reply to: guyh

I found the problem. The line that used to read: 

 

 

iProperties.Value(docFile.DisplayName, "Project", "Description") = iProperties.Value(docFile.DisplayName, "Custom", "DISPLAY_NAME")

 

Now reads: 

 

iProperties.Value(docFName, "Project", "Description") = iProperties.Value(docFName, "Custom", "DISPLAY_NAME")

 

Because I did not take into consideration that your part names might be balanced, it was attempting to locate the iproperties of an instance it didn’t recognize.

 

If you right click and open your part (from the assembly), you’ll see that the root name is without any file extension. It would call on that DisplayName, but because it is neither a file (as it has no extension), nor is it an instance (because it would need a “:#” behind it to indicate the occurrence), it just freaks out.

 

Needless to say, the code above fixes this by looking at the actual file path of the document and shortening it down to just the file name of the part. That way it should always pull correctly… I hope.

This was actually a lesson for me! Pull from the darn part file name forever!

 

New iLogic Rule is below. 


 

 

Dim openDoc As Document
openDoc = ThisDoc.Document

Dim docFile As Document

If openDoc.DocumentType = 12291 Then

	For Each docFile In openDoc.AllReferencedDocuments

		If docFile.DocumentType = 12290 Then

			Dim assemblyDoc As AssemblyDocument
			assemblyDoc = openDoc

			Dim assemblyDef As AssemblyComponentDefinition
			assemblyDef = assemblyDoc.ComponentDefinition

			Dim partDoc As PartDocument
			partDoc = ThisApplication.Documents.Open(docFile.FullFileName, False)
			
			Dim FNamePos As Long
            FNamePos = InStrRev(docFile.FullFileName, "\", -1)
            
            Dim docFName As String
            docFName = Mid(docFile.FullFileName, FNamePos + 1, Len(docFile.FullFileName) - FNamePos)
			
			iProperties.Value(docFName, "Project", "Description") = iProperties.Value(docFName, "Custom", "DISPLAY_NAME")
			Else 
			'''iProperties.Value(docFile.DisplayName, "Project", "Description") = "Assembly" 
			'''Don't really know if you want anything to happen if there is a Sub Assembly that this code comes across. 
			'''If not, you could just take this entire else portion out. 
		End If 
		
	Next
	
	Else
	
	MessageBox.Show("You must have a valid Assembly document open before using this code!", "File Type Mismatch!",MessageBoxButtons.OK,MessageBoxIcon.Exclamation)
	
End If 

 



If my solution worked or helped you out, please don't forget to hit the kudos button 🙂
iLogicCode Injector: goo.gl/uTT1IB

GitHub
Message 14 of 21
guyh
in reply to: MegaJerk

Great, thanks!  works a treat.  Glad you were able to learn something as well Smiley Wink  (Joke) 

 

Really appreciate the help.  I am going to study the rule a bit and improve my knowledge.

 

Cheers,

Guy

AutoCADM 2011 SP2
Inventor Pro 2011 SP2
Message 15 of 21
MegaJerk
in reply to: guyh

Improved less awful code : 

 

Dim openDoc As Document
openDoc = ThisDoc.Document

Dim docFile As Document 

If openDoc.DocumentType = 12291 Then

	For Each docFile In openDoc.AllReferencedDocuments
	
		If docFile.DocumentType = 12290 Then

			Dim FNamePos As Long
            FNamePos = InStrRev(docFile.FullFileName, "\", -1)			
            
            Dim docFName As String
            docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) - FNamePos)
		
			iProperties.Value(docFName, "Project", "Description") = iProperties.Value(docFName, "Custom", "DISPLAY_NAME")	
		
		End If 
		
	Next
	
	Else
	
	MessageBox.Show("You must have a valid Assembly document open before using this code!", "File Type Mismatch!",MessageBoxButtons.OK,MessageBoxIcon.Exclamation)
	
End If 

 



If my solution worked or helped you out, please don't forget to hit the kudos button 🙂
iLogicCode Injector: goo.gl/uTT1IB

GitHub
Message 16 of 21
guyh
in reply to: MegaJerk

Thanks, I'll give it a try.  I want to try and adapt it a bit to skip parts where the custom property is blank (the old files already have the correct description in the description field.)

 

Cheers,

Guy

AutoCADM 2011 SP2
Inventor Pro 2011 SP2
Message 17 of 21
guyh
in reply to: guyh

Ok so I added a little bit of code.  It seems to work.

 

Dim openDoc As Document
openDoc = ThisDoc.Document

Dim docFile As Document 

If openDoc.DocumentType = 12291 Then

	For Each docFile In openDoc.AllReferencedDocuments
	
		If docFile.DocumentType = 12290 Then
		
		
			Dim FNamePos As Long
            FNamePos = InStrRev(docFile.FullFileName, "\", -1)	
						         
            Dim docFName As String
            docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) - FNamePos)
			
			If iProperties.Value(docFName, "Project", "Description")="" Then
		
			iProperties.Value(docFName, "Project", "Description") = iProperties.Value(docFName, "Custom", "DISPLAY_NAME")	
			
			
			End If
		
		End If 
		
	Next
	
	Else
	
	MessageBox.Show("You must have a valid Assembly document open before using this code!", "File Type Mismatch!",MessageBoxButtons.OK,MessageBoxIcon.Exclamation)
	
End If 

 

AutoCADM 2011 SP2
Inventor Pro 2011 SP2
Message 18 of 21
MegaJerk
in reply to: guyh

Looks like it's all sorted out then! 



If my solution worked or helped you out, please don't forget to hit the kudos button 🙂
iLogicCode Injector: goo.gl/uTT1IB

GitHub
Message 19 of 21
DeerSpotter
in reply to: guyh

Here is a better Method to those in the Future

iLogicVb.RunRule("component name", "ilogic rule")
ThisDoc.Document.Rebuild()
Image and video hosting by TinyPic
..........................................................................................................................
Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.
..........................................................................................................................


See My LinkedIn Profile
Message 20 of 21
kevinDMTWZ
in reply to: MegaJerk

Hi, just seen one of your posts about running an ilogic rule from assembly level in each part. I'm trying to do something similar but with external rules. Basically I've started with your code for opening each part in the assembly but I can't then get to the next step. 

 

What I want to do is have every part open (in the background) and then trigger another external rule to run in each part. I can see in the ilogic window that there is a "RunRule in component" option but I don't know how to get that to trigger an external rule - if that is even possible.

 

Any input would be greatly appreciated!!

 

Cheers,

 

Kevin

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report