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: 

Add Rule to All Parts in an Assembly

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
phillip.shields
2641 Views, 6 Replies

Add Rule to All Parts in an Assembly

I have a three part question. First, I have an iLogic routine in an assembly that opens all the parts within the assembly, runs a particular rule, and then closes that part. Is there anyway I can get this to run in the background. I don't want my users to see all of my part files opening and closing. Here is my code:

 

'assign path values in iproperties to be equal
iProperties.Value("RD FLANGE:1","Custom", "FilePATH") = iProperties.Value("Custom", "FilePATH")

Dim FilePATH As String = "FilePATH"

customPropertySet = ThisDoc.Document.PropertySets.Item _
("Inventor User Defined Properties")

Try

         prop= customPropertySet.Item(FilePATH)

Catch

            customPropertySet.Add("", FilePATH)

End Try

If iProperties.Value("Custom", "FilePATH") = "" Then

iProperties.Value("Custom", "FilePATH") = "C:\"

Else

End If

Dim partDoc As PartDocument

FilePATH = InputBox("Enter a FilePATH for part file", "iLogic", iProperties.Value("Custom", "FilePATH"))

iProperties.Value("Custom", "FilePATH") = FilePATH

'Define the open document
Dim oDoc As Document
oDoc = ThisDoc.Document

'Look at all of the files referenced in the open document
Dim docFile As Document
For Each docFile In oDoc.AllReferencedDocuments

'open the indexed file
'true opens the file normaly
'false opens the file programatically without generating the graphics
ThisApplication.Documents.Open(docFile.FullFileName, True)

'run rule in the drawing document
auto = iLogicVb.Automation
auto.RunRule(docFile, "dxf")

'you may also save And close this drawing document
docFile.Save
docFile.Close
	
Next

iLogicVb.UpdateWhenDone = True

 

My second question is how do I add a routine that as the part files are opened checks to see if "MyRule" is there. If "MyRule" is not there add "MyRule" else if "MyRule" exist move on to the next part.

 

My third and final question is there anyway for my code to distinguish between standard parts and sheet metal parts. Basically, if the opened part is not a sheet metal part then I do not want "MyRule" to be inserted and I do not want "MyRule" to be run. I want it to move on to the next part within the assembly. Any help on this will be greatly appreciated.

6 REPLIES 6
Message 2 of 7
MegaJerk
in reply to: phillip.shields

You should be able to open a file invisibly by changing this : 

 

ThisApplication.Documents.Open(docFile.FullFileName, True)

 To This : 

ThisApplication.Documents.Open(docFile.FullFileName, False)

 

Because of this reason (taken from the API documentation) : 

 

OpenVisible  Optional input Boolean that specifies whether to open the document as visible. 
 

-- You actually seem to have a comment inside of your code that states the above 😉 you wouldn't know anything about that now would you 🙂 🙂


----

Moving on to your other questions, you can do what you're talking about, but I wonder if just creating an external rule wouldn't be a more simple solution. 

The sample below is of a rule that is placed inside of your main assembly. The external rule that you will create will be listed below that. 

Assembly Rule ::: 

''' We need to tell this rule that we'll be referencing
''' something from a file outside of this rule. 
''' In this instance, that file is an External Rule named
''' ExternalRuleTest2.iLogicVb


AddVbFile "ExternalRuleTest2.iLogicVb" 

''' Before we start creating Objects, let's make sure that the user 
''' has an Assembly Document open and active! 

If Not ThisApplication.ActiveDocument.DocumentType = kAssemblyDocumentObject Then 
	Return
End If

''' Now that we know we're inside of an Assembly Document
''' we can outright declare an AssemblyDocument Object, 
''' and set said object to the Active Document.

Dim aDoc As AssemblyDocument
aDoc = ThisApplication.ActiveDocument


''' Now let's make an empty Document Object named iDoc
Dim iDoc As Document

''' This should be familiar teritory for you. 
''' We just want to loop through all of the referenced
''' documents found inside of our currently active assembly

For Each iDoc In aDoc.AllReferencedDocuments

	''' Let's check to see what sort of SubType our document is. 
	''' SubTypes are pre-defined, and so we know that the SheetMetal
	''' subtype will return a GUID of -----
	''' "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" 
	''' Using this knowledge, we can check beforehand what sort
	''' of SubType our document is. Based on that, we can do some 
	''' conditional work. 
	
	If iDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then 
	
		''' If the SubType is SheetMetal then we can call out this
		''' Public Function (called - ShowDocType) from our 
		''' External Rule (which we pointed to at the beginning of
		''' this rule. 
		ShowDocType(iDoc)
	End If 
Next

 

I've added comment code here so that when you copy / paste this into Inventor, it should come out all pretty like. 

Basically this is just iterating through the Referenced Documents, and sending off the documents that are for certain Sheet Metal parts off to the external rule for some additional work. 

The External Rule is as follows ::: 

''' let's make this all rather public. 
Public Module ExternalRuleTest2
	''' We're going to need a means of processing our incoming
	''' Inventor Document, so here I've made a public Function. 
	''' In this case, our Function is named 'ShowDocType'
	''' which you should remember from the Rule used inside of
	''' of the Assembly. 
	''' This Function requires 1 parameter, in this case, an
	''' Inventor Document. 'oDoc' is the name given to that
	''' object. 
	''' Though near the end, you see that it returns a String 
	''' I have not included anything to give a value
	''' to that String, as I have no need to send information back
	''' to the main Assembly rule (for this example at least).
	
	Public Function ShowDocType(oDoc As Inventor.Document) As String 
		''' inside of here, we're just going to do some real simple
		''' queries of this document. Nothing is being written as of yet
		''' but we certainly could if we wanted to.

		MsgBox(oDoc.DocumentType.ToString() & " --- " & oDoc.SubType)
		
		''' Try writing out some changes to the document here! 
		''' Remember to check and see if the file is writable beforehand!
		
	End Function 
End Module 

 


Remember that when working with External rules that will be accessed for their delicious Public Functions, you should go to the Options located in the External Rule, and check the 'Straight VB Only' button. 

Instead of having to worry about placing a rule inside of a file if it isn't there, you can just keep your one external rule up to date, and have it do the work. Also because you can filter out what gets passed to the external rule, you can make sure that only the select type of files that you want to be processed, get processed. 

If you have any questions  further about applying this method to your workflow, feel free to ask!!! 

The more water the higher the boat! 



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 7
phillip.shields
in reply to: MegaJerk

Thank you for your very quick and thorough response. Everything is working perfectly. The only thing I would like to do is add a rule inside of each sheet metal part within my assembly and to skip over that part if the rule already exist. Then I would like to run that rule. Also,  for whatever reason when I change:

 

ThisApplication.Documents.Open(docFile.FullFileName, False)

 

I get this error: 

 

a.png

 

I assume this means my part has to be open for my code to execute which is why it remained "True".

Message 4 of 7
phillip.shields
in reply to: MegaJerk

Don't worry about the part in my reply where I wanted to add "MyRule" to each part. I just now realized that you suggested creating an external rule instead of trying to add it to each part file. My bad.

Message 5 of 7
MegaJerk
in reply to: phillip.shields

It's odd that you're getting that error. I will do a little more testing to see if I can get things to work on my end. i'll report back once I have some data.



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

GitHub
Message 6 of 7
phillip.shields
in reply to: MegaJerk

Ok thanks that would be great.

Message 7 of 7
shmartin89
in reply to: MegaJerk

Hi guys,

 

I have managed to use this rule from my drawings file to all parts. Getting the same problem when I type 'False' to open all documents. Was a solution ever found for this so all the files dont have to be opened?

 

Sandy

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

Post to forums  

Autodesk Design & Make Report