- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
iLogic Rule works on some Sheetmetal Parts but not others....
Hi Everybody,
i have an iLogic Rule that i want to run from a drawing. The rule will open the referenced part, Save it and close it...that simple(code shown below). Problem is, it doesn't work on all parts. Any advise would be greatly appreciated.
Dim doc As DrawingDocument = ThisDoc.Document Dim sheet As Sheet = doc.ActiveSheet Dim view As DrawingView = sheet.DrawingViews.Item(1) Dim refDoc As Document = ThisApplication.Documents.Open(view.ReferencedFile.FullFileName) refDoc.Save() refDoc.Close()
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Would this work for you instead?:
Dim oDDoc As DrawingDocument = ThisDrawing.Document
Dim oMDoc As Document = ThisDrawing.ModelDocument
oMDoc.Save
oMDoc.CloseIf this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE'
.
If you have time, please... Vote For My IDEAS
and Explore My CONTRIBUTIONS
Inventor 2021 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum
Wesley Crihfield
(Not an Autodesk Employee)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Or perhaps this:
Dim oDDoc As DrawingDocument = ThisDrawing.Document
Dim oSheet As Inventor.Sheet = oDDoc.ActiveSheet
Dim oView As DrawingView = oSheet.DrawingViews.Item(1)
Dim oViewDoc As Document = oView.ReferencedDocumentDescriptor.ReferencedDocument
oViewDoc.Save
oViewDoc.CloseIf this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE'
.
If you have time, please... Vote For My IDEAS
and Explore My CONTRIBUTIONS
Inventor 2021 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum
Wesley Crihfield
(Not an Autodesk Employee)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Do you mean it doesnt work in all parts that is also used as reference in the same sheet? If so, it because the code is only refering to the first item. It needs to iterate through all drawing references.
Dim view As DrawingView = sheet.DrawingViews.Item(1) 'works only in the first item.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
if you want to save all documents referenced (direct and indirect) by the drawing you could do this.
For Each refDoc As Document In ThisDoc.Document.AllReferencedDocuments refDoc.Save() Next
Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Blog: hjalte.nl - github.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
@Anonymous
@Anonymous wrote:Do you mean it doesnt work in all parts that is also used as reference in the same sheet? If so, it because the code is only refering to the first item. It needs to iterate through all drawing references.
No, i mean when I run this rule on one drawing/model it works perfectly. When i run it on a completely different drawing connected to a different model, it doesn't work. i get the error shown below:
""Error in rule: SM_Drawing_Processing, in document: D04A01723-1__ATL_000.idw
Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))"
Error in rule: SM_Drawing_Processing, in document: D04A01723-1__ATL_000.idw
Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
@JelteDeJong wrote:if you want to save all documents referenced (direct and indirect) by the drawing you could do this.
For Each refDoc As Document In ThisDoc.Document.AllReferencedDocuments refDoc.Save() Next
This is interesting to my newbie brain! Can i used this in an assembly to iterate thru each part?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Yes. Using the document's AllReferencedDocuments property like that is one of the most common ways to iterate through all documents within an assembly. It is often more efficient than when iterating through the assembly's ComponentOccurrences, because you can have multiple ComponentOccurrences that all represent the same source document.
Here is another way to iterate through each sheet, and each view within each sheet of a drawing, and getting the document being represented within those views. However, if the same document is being represented within multiple views, your Save() code may run on that document multiple times.
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
MsgBox("This rule '" & iLogicVb.RuleName & "' only works for Drawing Documents.",vbOKOnly, "WRONG DOCUMENT TYPE")
Return
End If
Dim oDDoc As DrawingDocument = ThisDrawing.Document
Dim oSheet As Inventor.Sheet
Dim oView As DrawingView
Dim oViewDoc As Document
For Each oSheet In oDDoc.Sheets
For Each oView In oSheet.DrawingViews
oViewDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
oViewDoc.Save()
Next
NextIf this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE'
.
Wesley Crihfield
(Not an Autodesk Employee)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
@WCrihfield wrote:Yes. Using the document's AllReferencedDocuments property like that is one of the most common ways to iterate through all documents within an assembly. It is often more efficient than when iterating through the assembly's ComponentOccurrences, because you can have multiple ComponentOccurrences that all represent the same source document.
Here is another way to iterate through each sheet, and each view within each sheet of a drawing, and getting the document being represented within those views. However, if the same document is being represented within multiple views, your Save() code may run on that document multiple times.
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
MsgBox("This rule '" & iLogicVb.RuleName & "' only works for Drawing Documents.",vbOKOnly, "WRONG DOCUMENT TYPE")
Return
End If
Dim oDDoc As DrawingDocument = ThisDrawing.Document
Dim oSheet As Inventor.Sheet
Dim oView As DrawingView
Dim oViewDoc As Document
For Each oSheet In oDDoc.Sheets
For Each oView In oSheet.DrawingViews
oViewDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
oViewDoc.Save()
Next
Next
This is so AWESOMMMEEE!! It fixes an issue i was ABOUT to try to address. The other issue is still an issue tho ![]()
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
It sounds like we need to know more details, before we can help you further.
When you post what the error message says, capture the text or screen captured image from both tabs of the error message. Often the second tab contains much more information, and may give us a clue as to where in the code it is having a problem.
Also, there are many different ways to "debug" (find and/or weed out potential errors) in code, and we may have to include one of these techniques here, to give us a better understanding of where,when, and why it is either throwing errors, or not working as expected. If you have Inventor 2019.1 (I believe) or later then you should have the iLogicLog tab available (or can be turned on), and the ability to write things to the iLogic log as your code starts, progresses, and ends (depending on how you set it up and what level of the Log you are using). Or as a "fast & loose" option, you could put something like MsgBox(1), or MsgBox(2)...etc after each line within your code, then when you run it, pay attention to the last number it shows before it throws an error, and that will tell you that the next line after that numbered MsgBox() is where it is having trouble.
Then there are ways to attempt to avoid expected errors. The current standard for this within iLogic (vb.net) is the Try...Catch...End Try block. Within the Try portion, you try to do what you want, then in the Catch portion you put what do do if the Try portion fails. This can be very useful to prevent your code from abruptly stopping because of an error, and if you property utilize the Catch portion, it can potentially give you very meaningful feedback as to what went wrong.
Here is an updated version of my earlier code that includes another "check" and a couple of Try...Catch...End Try blocks in there, that I'm hoping will offer some meaningful feedback. Let me know how this works out for you, and any messages you're seeing if things don't fully work.
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
MsgBox("This rule '" & iLogicVb.RuleName & "' only works for Drawing Documents.",vbOKOnly, "WRONG DOCUMENT TYPE")
Return
End If
Dim oDDoc As DrawingDocument = ThisDrawing.Document
Dim oSheet As Inventor.Sheet
Dim oView As DrawingView
Dim oViewDoc As Document
For Each oSheet In oDDoc.Sheets
If oSheet.DrawingViews.Count > 0 Then
For Each oView In oSheet.DrawingViews
Try
oViewDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
Catch
MsgBox("A Model document couldn't be retrieved from the following View:" & vbCrLf & _
"Sheet: [" & oSheet.Name & "] View: [" & oView.Name, vbOKOnly + vbInformation, " ")
Continue For
End Try
Try
oViewDoc.Save()
Catch
MsgBox("We've found the Model document in the following View:" & vbCrLf & _
"Sheet: [" & oSheet.Name & "] View: [" & oView.Name & vbCrLf & _
"But the attempt to simply Save (not SaveAs) failed.", vbOKOnly + vbCritical, " ")
End Try
Next
End If
Next
If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE'
.
Wesley Crihfield
(Not an Autodesk Employee)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
@Anonymous maybe there is an error on your referenced model that needs to be addressed before it allows saving. or maybe an event trigger.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Ok here's the whole spill ![]()
I have rule that does the stuff i need to do. From a drawing:
- checks to make sure the referenced model matches the drawing
- opens the referenced model and saves. This will trigger two external rules
- updated everything to make sure the dxf will be accurate.
- creates the dxf.
- Closes the model and goes back to the idw
PDF's the drawings.
This is the code for the checking portion of the rule
Imports System.Threading.Tasks Dim timeout = 1.5 ' secs Dim msg As New Form() With { .Enabled = False } Dim doc As DrawingDocument = ThisDoc.Document Dim sheet As Sheet = doc.ActiveSheet Dim view As DrawingView = sheet.DrawingViews.Item(1) Dim refDoc As Document = ThisApplication.Documents.Open(view.ReferencedFile.FullFileName) 'Dim addin As ApplicationAddIn 'xxxxxx'Dim iLogicAuto As Object = addin.Automation'xxxxxx 'oDoc = ThisDoc.FileName(False) 'without extension'xxxxxx attachedModel = ThisDoc.Path & "\" & IO.Path.GetFileName(ThisDrawing.ModelDocument.FullFileName) PreModel = ThisDoc.ChangeExtension(".ipt") intendedModel = Replace(PreModel,"-1_","-0_") 'MessageBox.Show("attachedModel = " & attachedModel, "iLogic") If attachedModel = intendedModel Then Task.Delay(TimeSpan.FromSeconds(timeout)).ContinueWith( Sub(t) msg.Close() End Sub , TaskScheduler.FromCurrentSynchronizationContext()) MessageBox.Show(msg,"Model/Drawing Matches", "iLogic") refDoc.Save() refDoc.Close() ' Parameter("d0") = 1.2 Else MessageBox.Show("The drawing has the wrong model attached", "iLogic") Return End If
This is the code that calls the model and pdf's after it is done.
Dim doc As DrawingDocument = ThisDoc.Document Dim sheet As Sheet = doc.ActiveSheet Dim view As DrawingView = sheet.DrawingViews.Item(1) Dim refDoc As Document = ThisApplication.Documents.Open(view.ReferencedFile.FullFileName) iLogicVb.RunExternalRule("ModelMatchDrawing") 'refDoc.Save()'refDoc.Close() iLogicVb.RunExternalRule("PDFit")
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
In both of those two codes, it looks like you need to replace:
Dim refDoc As Document = ThisApplication.Documents.Open(view.ReferencedFile.FullFileName)with this:
Dim refDoc As Document = ThisApplication.Documents.Open(view.ReferencedDocumentDescriptor.FullDocumentName)because the 'view' object (DrawingView) doesn't support ReferencedFile directly.
Plus, the 'Open' function actually prefers the FullDocumentName vs the FullFileName (though usually not necessary unless dealing with documents within an assembly).
Here are the links for those objects.
DrawingView.ReferencedDocumentDescriptor Property
Wesley Crihfield
(Not an Autodesk Employee)