Announcements
Due to scheduled maintenance, the Autodesk Community will be inaccessible from 10:00PM PDT on Oct 16th for approximately 1 hour. We appreciate your patience during this time.
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: 

RunExternalRule AllReferencedDocuments

7 REPLIES 7
Reply
Message 1 of 8
Anonymous
720 Views, 7 Replies

RunExternalRule AllReferencedDocuments

Hello, I would like to be able to run an external rule file  (Z:\RuleTest.iLogicVb) within all of the parts of a selected assembly. I can't seem to find the line of code to do so. From what I seem to understand, the iLogicVb.RunExternalRule seems to want to use the displayed name in the list, so "A:1" instead of "A.ipt" or "Z:\Project\A.ipt" which is where it is causing me issues. I am not a master in iLogic, I tried a few things, but it doesn't work. It also fails if I have files from the Content Center because they do not have a .ipt for the DisplayName, so it removes 4 characters which shouldn't be removed...

 

Thanks for any help !

 

Dim openDoc As Document
openDoc = ThisDoc.Document

Dim docFile As Document

If openDoc.DocumentType = 12291 Then

    For Each docFile In openDoc.AllReferencedDocuments

            Dim OccName As String
            OccName = Left(docFile.DisplayName, Len(docFile.DisplayName) - 4) & ":1"
			
            MsgBox(docFile.DisplayName)
			iLogicVb.RunExternalRule(OccName, "Cartouche")

    Next
    
End If 

 

7 REPLIES 7
Message 2 of 8
WCrihfield
in reply to: Anonymous

When the external iLogic rule runs, it will only be acting on whichever document was active when the rule was ran, unless the rule itself is set up to only work with a specified file, where the FullFileName was supplied, within the target rule.  So, you would have to open and/or activate each document, just before running the external rule.

You don't need to supply the full path of the external rule if it is stored where your iLogic Configuration Options is set-up for.  And no, you can't specify which document to run on within the call to run the external rule. It will only except rule arguments, which is a way to share data between rules.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 8
WCrihfield
in reply to: Anonymous

Try this:

I wasn't sure if your external rule was called "iLogicVb" or "RuleTest.iLogicVb".

 

Dim oADoc As AssemblyDocument = ThisAssembly.Document
For Each oRefDoc As Document In oADoc.AllReferencedDocuments
	If oRefDoc.Open = False Then
		ThisApplication.Documents.Open(oRefDoc.FullDocumentName, False)
	End If
	oRefDoc.Activate
	iLogicVb.RunExternalRule("iLogicVb")
Next

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 8
Anonymous
in reply to: WCrihfield

Thank you, after digging through here a bit, I found a bit of code that works, but now I am wondering if there would be a faster way to make it work the way I want it to.

 

Basically, here is my issue, I am looping through all of the files in an assembly and then opening up an Excel spreadsheet to retrieve data in it, I ASSUME that this is why it takes so long to run the code.

 

Would it be possible for me to assign the values of each Excel lines to variables inside of my main code (the one that does the looping) and then pass a value called "ColorOfPart" to the RunExternalRule which would then analyze that "ColorOfPart" and do stuff with it?

 

This is the code:

 

Dim aDoc As AssemblyDocument
aDoc = ThisApplication.ActiveDocument
Dim iDoc As Document
For Each iDoc In aDoc.AllReferencedDocuments
auto = iLogicVb.Automation
auto.RunExternalRule(iDoc, "Z:\Inventor 2014\Gabarit_par_defaut\Paints")
Next
iLogicVb.UpdateWhenDone = True

 

Message 5 of 8
WCrihfield
in reply to: Anonymous

So, are you talking about opening up a different Excel file that relates to each of the referenced file within the assembly?  Or is all the Excel information coming from one Excel file?

If we're dealing with one Excel file, yes we may be able to just open the Excel file once, then either use references to data within it that varies as we loop through the documents, or we may extract all info needed before entering the loop, then apply/use that data within the loop.

Can you tell me a little more details about the whole process, so I can better understand and help you?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 8
Anonymous
in reply to: WCrihfield

Hi, sorry for the delay.

I came up with a code that is semi decent for now, but it is a bit slow.

So here is what I would like to do:

 

Open the main assembly, run the embedded iLogic rule.

The rule would do the following things:

1- Read lines 2 through 11 of an external excel spreadsheet

2- Keep the data in separate variables (Line1, Line2, Line3, Line4...)

3- Loop through the assembly for INDIVIDUAL files (I don't want it to open 10 times the same part if it is 10 times in the assembly, just open it once)

4- Create variables in that file it is reading and change its actual color based on an internal variable called "TypePiece" that would use Line1 Line2 Line3 Line4 depending on what "TypePiece" is (for instance; Principale would call Line1, Galvanized would call Line2, Guards would call Line3, etc.) and if the file is an assembly or part (not sheet metal), return a MsgBox saying "This can't be applied to this part. Continuing."

Message 7 of 8
WCrihfield
in reply to: Anonymous

This is what I've put together so far.  It's not complete because there are several things I don't know, and I don't have the files to test with.  I put a bunch of comments within the code that you can get rid of.  I was just making suggestions or asking questions, because I'm pretty much shooting in the dark here.

AddReference "Microsoft.Office.Interop.Excel.dll"
Imports Microsoft.Office.Interop.Excel

Dim oXLFile As String = "C:\Temp\Excel File.xlsx"
Dim oSheet As String = "Sheet1"
Dim oExcel As New Microsoft.Office.Interop.Excel.ApplicationClass
oExcel.DisplayAlerts = False
oExcel.Visible = False
Dim oWB As Workbook = oExcel.Workbooks.Open(oXLFile)
Dim oWS As Worksheet = oWB.Worksheets.Item(oSheet)
Dim oCells As Range = oWS.Cells

'Assuming Rows 2 through 11 contain the needed data
'Assuming only the first cell in each row contains the needed data.  I have no idea.
'Assiming their values are String type values
'If the values are supposed to be a different data type, you may need to change how the variables are defined
Dim oLine2 As String = oCells.Item(2, 1).Value
Dim oLine3 As String = oCells.Item(3, 1).Value
Dim oLine4 As String = oCells.Item(4, 1).Value
Dim oLine5 As String = oCells.Item(5, 1).Value
Dim oLine6 As String = oCells.Item(6, 1).Value
Dim oLine7 As String = oCells.Item(7, 1).Value
Dim oLine8 As String = oCells.Item(8, 1).Value
Dim oLine9 As String = oCells.Item(9, 1).Value
Dim oLine10 As String = oCells.Item(10, 1).Value
Dim oLine11 As String = oCells.Item(11, 1).Value
oWB.Close(False)
oExcel.Quit
oCells = Nothing
oWS = Nothing
oWB = Nothing
oExcel = Nothing

Dim oADoc As AssemblyDocument = ThisAssembly.Document
Dim oRefDoc As Document
Dim oCProps As PropertySet
Dim oAuto As IiLogicAutomation = iLogicVb.Automation
For Each oRefDoc In oADoc.AllReferencedDocuments
	'You could just set this up so if it isn't the right document type & SubType it will just go to the next document, without messages.
	If oRefDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("This is an Assembly, so this can't be applied to it. Continuing.")
		Continue For
	ElseIf oRefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
		If oRefDoc.PropertySets.Item("Design Tracking Information").Item("Document SubType Name").Value <> "Sheet Metal" Then
			MsgBox("This is a Part, but not a Sheet Metal Part, so this can't be applied to it. Continuing.")
			Continue For
		Else
			'I assume by 'create variables within the document', you mean create custom iProperties within it?
			oCProps = oRefDoc.PropertySets.Item("Inventor User Defined Properties")
			'Check to see if those custom iProperties already exist
				'If so, set their values to the new values from Excel
				'If not, create new custom iProperties and set their values to the new values from Excel
			'Check to see if the "TypePiece" custom iProperty exists
				'If it does, use its value to determine the correct color for the model
				'If it doesn't exist, let user know MsgBox(), then maybe create it.
			oAuto.RunExternalRule(oRefDoc, "Z:\Inventor 2014\Gabarit_par_defaut\Paints")
			'I assume this external iLogic rule is taking care of searching for the pre-existing 'variable' called "TypePiece", then changing the color accordingly?
			'I'm not sure how you want to use the data from the Excel file here.
		End If
	End If
Next

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 8
halooween123
in reply to: Anonymous

How to add a condition that if a part already has a rule, the rule will not run with that part

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

Post to forums  

Autodesk Design & Make Report