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: 

Running a rule after opening a drawing with a different rule

8 REPLIES 8
Reply
Message 1 of 9
dleesuk
233 Views, 8 Replies

Running a rule after opening a drawing with a different rule

Hello again,

 

I have a new dilema.  🤣

 

I have a rule which opens a specific drawing from within a different drawing environment.

This works very well.

 

The problem I have is that I want to run another rule on the newly opened drawing which will save the sheets within the drawing out as DXFs.

 

Separately, these routines work very well...

  • The open drawing rule within another drawing performs as expected.
  • The rule which saves the sheets out as DXFs performs as expected.

However, when I call the rule to save the DXFs on the file that has been opened, I get the following error.

  • Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))

The description of the error is incredibly vague. I have tried to find a solution online, but to no avail.

 

This is my OPEN DXF DOC code, which works well in isolation.

I strip the relevant text from the first drawing title to help form the location of the file to be opened (the DXF files are always in similar location as the main drawing and similarly named)

Also, I use different file locations for testing purposes

 

AddReference "System.IO"
Dim oDoc As Document
oDoc = ThisApplication.ActiveDocument

'oDxfDwgPath = "C:\VaultWS\Design\"  'LIVE
'oDxfDwgPath = "C:\Users\Darren Lees\OneDrive\CAD\iLogic\DXF Testing\Design\"  'TESTING
oDxfDwgPath = "C:\Users\Darren Lees\OneDrive - Osbit\iLogic\DXF Testing (OSBIT)\Design\"  'TESTING DRIVE (OSBIT)
'MessageBox.Show("Document path = " & oDxfDwgPath, "iLogic")


'Get the active drawing file name
oFileName = ThisDoc.FileName(False) 'no extension

oFolderName = Mid(oFileName, 1, 7)  'strip to get the correct folder ID

'The full file name and location
oFullFileName = oDxfDwgPath & oFolderName & "\" & oFileName & "_DXF.dwg"  'add DXF for correct filename
'MessageBox.Show("Document name = " & oFullFileName, "iLogic")

'Open the dwg file
If System.IO.File.Exists(oFullFileName)
	ThisApplication.Documents.Open(oFullFileName, True)
Else
	MessageBox.Show("DXF Document isn't available locally." & vbCrLf & "Please download it from Vault and try again.", "iLogic")
	Return
End If

iLogicVb.RunExternalRule("DXF START PROCESS")    '<---- I want to run this on the DXF drawing once it is open

 

I fear this might be a system thing rather than an iLogic thing, which will be WAY beyond my current skillset.  

 

Any ideas??

 

Many thanks in advance


Regards

Darren
8 REPLIES 8
Message 2 of 9
A.Acheson
in reply to: dleesuk

Hi @dleesuk 

 

What is likely wrong here is the targeting document object on the opened drawing. The more info tab of the error message will give you better information than the first tab. If you are using ilogic version of ThisDoc.Document this will be the document you launched the external rule from and not the document just opened.. Can you share the external rule to export dxf?

Here is a sample external rule to see the difference in document object used. If you are using further ilogic short snippets to get file names etc these may also fail. 

 

Logger.Info("ThisDoc-FileName: " & ThisDoc.Document.FullFileName)
Logger.Info("ThisApplication-FileName: " & ThisApplication.ActiveDocument.FullFileName)

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 3 of 9
dleesuk
in reply to: A.Acheson

Hi @A.Acheson,

 

The More Info tab says this:

System.NullReferenceException: Object reference not set to an instance of an object.
at ThisRule.Main() in external rule: DXF START PROCESS:line 12
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

This is all Greek to me!!


Regards

Darren
Message 4 of 9
dleesuk
in reply to: dleesuk

Hi @A.Acheson,

 

This is the next Rule in the chain which doesn't run correctly. It renames the sheet names so I have real filenames for the DXFs.

 

Te next rule (FILE LOC FOR DXF) creates all of the file locations for DXFs, PDFs and a ZIP file, though this isn't relevant here..

 

This rule was built with a lot of help from this Forum, for which I'm eternaly grateful!!  😁

oDoc = ThisDoc.Document
oKeywords = iProperties.Value("Summary", "Keywords")

If oKeywords = "DXF" Then
	'Find all selected occurences and add them to an ObjectCollection
	Dim oDrawingDims As DrawingDimension
	Dim oFileName As String
	oSheets = oDoc.Sheets

	Try
		For Each oSheet In oSheets
			oSheet.ExcludeFromCount = True
			oSheet.activate
			oView = oSheet.DrawingViews.Item(1)

			'Loop through all the dimensions
			For Each oDrawingDims In oDoc.ActiveSheet.DrawingDimensions
				'set to reference dims
				oDrawingDims.Tolerance.SetToReference
			Next

			modelName = oView.ReferencedDocumentDescriptor.ReferencedDocument

			oPropPartNo = modelName.PropertySets.Item("Design Tracking Properties")
			oPropItemNo = modelName.PropertySets.Item("Inventor User Defined Properties")
			oPropRevNo = modelName.PropertySets.Item("Summary Information")

			ActiveSheet.Sheet.Name = oPropPartNo.Item("Part Number").Value + "_" + oPropRevNo.Item("Revision Number").Value + " (Item " + oPropItemNo.Item("ItemNo").Value + ")"
		Next
	Catch
		MessageBox.Show("An error has occured. Please contact the administrator.", "ERROR!!")
		Exit Sub
	End Try
Else
	MessageBox.Show("Application can only be used with DXF drawings." & vbCrLf & "Please open a valid DXF drawing and try again!", "Incorrect drawing type!",MessageBoxButtons.OK,MessageBoxIcon.Exclamation)
	Return
End If

iLogicVb.RunExternalRule("FILE LOC FOR DXF")

oSheets(1).activate

 

I sort of figured it had something to do with the start document, but I don't know how to figure it out.

 

Thank you for your help so far

 


Regards

Darren
Message 5 of 9
A.Acheson
in reply to: dleesuk

These two lines are ilogic API code. These are the issue. They are referring to the document you launch the rule from and not the one just opened. 

oDoc = ThisDoc.Document
oKeywords = iProperties.Value("Summary", "Keywords")

 

So to run the external rules in a chain sequence you will need to change where the document object is coming from. So once you open the drawing you want to detect it is the active document. The keyword iProperty now comes from the Inventor API Property Set. Same thing just accessed differently. 

 

Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument

Dim sKeywords As String = oDrawDoc.PropertySets.Item("Inventor Summary Information").Item("Keywords").Value 
Logger.Info(sKeywords)

 

 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 6 of 9
WCrihfield
in reply to: dleesuk

Hi @dleesuk.  Another option in a situation like this, is to actually 'send' the Document object reference from the first rule to the second rule, to make absolutely sure that it will be working with the same document that the first rule just opened.  But to implement that option, you would need to make some preparations (minor changes) to both rules.  The first rule would need to create a NameValueMap, then add an entry into it, like oMap.Add("Document", oOpenedDoc), where the entry's Name indicates that its value will contain a Document type value.  Then include that NameValueMap as the optional second input in your line of code that runs the other rule.  Then at the beginning of the second rule, you would need to prepare it to either 'receive' a document through the RuleArguments, or get it through normal means, if that is not available (nothing was sent to it).  

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 9
dleesuk
in reply to: dleesuk

So, thank you all so far for your input.

 

I have tried @A.Acheson's method and it sort of works. 

I can open the drawing and run the rule, but the rule runs on the opening drawing, not the opened, so I get the error regarding "...only be used with DXF drawings...".  I removed the IF/THEN aspect and ran it again and got DXFs of the fabrication drawing (opening), not the DXF drawing (opened).

 

And, I'm not sure where to start with @WCrihfield's possible solution.  I have used NameValue mapping before, but only for variables. Not sure what to actually send and where to.

 

Thank you so far!!! 


Regards

Darren
Message 8 of 9
WCrihfield
in reply to: dleesuk

Hi @dleesuk.  For starters, you will need to capture the document object that you are opening within the first rule.  To do that, first create a Document type variable outside of that If...Then block that you are opening it within.

Dim oOpenedDoc As Inventor.Document = Nothing

...then change that one line like this:

oOpenedDoc = ThisApplication.Documents.Open(oFullFileName, True)

...then, just before running the other rule at the end of the first rule...

Dim oArgs As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
oArgs.Add("Document", oOpenedDoc)
iLogicVb.Automation.RunExternalRuleWithArguments(oOpenedDoc, "DXF START PROCESS", oArgs)

That will prepare the first rule to send that document to the other rule, and help in two different ways to ensure that the external rule focuses on that specific document.  The first input variable is to specify which document you want the rule to focus on, then you are also sending the document reference to that rule also.

 

Then in the other rule, instead of that first line:

oDoc = ThisDoc.Document

... you will need to prepare it to receive that document you sent to it.

Dim oDoc As Inventor.Document = Nothing
If RuleArguments.Exists("Document") Then
	oDoc = RuleArguments.Value("Document")
Else
	oDoc = ThisDoc.Document
End If

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 9 of 9
darren.lees
in reply to: dleesuk

Thank you @WCrihfield .  I'll certainly give this a go.  It looks complex, but once it's in place and I can fully see the context, then I'm sure it will be relatively easy to understand.

 

Watch this space.

 

Thanks again 😊

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

Post to forums  

Technology Administrators


Autodesk Design & Make Report