Inventor API save drawingdocument gives error in iLogicRule

Inventor API save drawingdocument gives error in iLogicRule

mowag
Enthusiast Enthusiast
803 Views
8 Replies
Message 1 of 9

Inventor API save drawingdocument gives error in iLogicRule

mowag
Enthusiast
Enthusiast

Hi,

 

In our drawingtemplate we run a iLogic Rule to set a custom property Scale with a trigger "Before Save Drawing".

When i save the drawingdocument in Inventor with the save button , no problem !

 

When i save the document with oDrawDoc.Save(), i have an error : No such interface supported ???

 

Screenshot_Save1.png

Screenshot_Save3.png

Screenshot_Save2.png

0 Likes
Accepted solutions (1)
804 Views
8 Replies
Replies (8)
Message 2 of 9

WCrihfield
Mentor
Mentor

Hi @mowag.  The error message from your C# code screen indicates that it is failing to cast or convert a system com object into a DrawingDocument type object.  That is an odd explanation, because there doesn't appear to be any code shown matching that description.  It seems to me like that error message may belong to an ilogic rule, instead of a C# code window.  In the iLogic rule you are showing, it is setting the value of a DrawingDocument Type variable with 'ThisApplication.ActiveDocument', which could potentially fail, if the true 'active' document was not a DrawingDocument.  That could cause that error to show.  You may want to incorporate a document type check in there to help avoid that potential error.

 

On the other topic...it is not a good idea to attempt to save a document within code that is triggered to run 'before document save', because it seems like that would cause a never ending cycle.  The document should go ahead and save when the rule finishes, because the rule was triggered to run 'before save'.  Just my thoughts on the situation.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 9

Zach.Stauffer
Advocate
Advocate

How are you initializing oDrawDoc in your C# code?

0 Likes
Message 4 of 9

mowag
Enthusiast
Enthusiast

Hi,

 

I've changed the Ilogic rule but i'll still get the same error ? But you had a good point !!

 

Your point of saving the document in code just before saving the document. I did not save the doc, but just did an DocumentUpdate, or am i wrong?

 

It occurs with old drawings who has this Ilogic rule.

In our new drawingtemplates, this rule is deleted.

 

Screenshot_Scale2.png

0 Likes
Message 5 of 9

mowag
Enthusiast
Enthusiast

Hi,

 

Initialisation on line number 16

        public void FindPartDrawing(string InventorDocFullFileName, ref Part Part, string PartNumber, string Description)
        {

            string path = System.IO.Path.GetFullPath(InventorDocFullFileName);
            string filename = System.IO.Path.GetFileNameWithoutExtension(InventorDocFullFileName);
            string drawingfilename = RootPathDrawings + "\\" + filename + ".dwg";

            if (System.IO.File.Exists(drawingfilename))
            {
                Part.Drawing.Material = Part.Material;
                Part.Drawing.Path = drawingfilename;
                Part.Anomaly = true;

                Inventor.DrawingDocument oDrawDoc;
                Inventor.DrawingSheetSizeEnum size;
                oDrawDoc = (Inventor.DrawingDocument)_inventordataService.invApp.Documents.Open(Part.Drawing.Path, false);
                Inventor.Sheet Sheet = oDrawDoc.ActiveSheet;
                size = Sheet.Size;
                switch (size)
                {
                    case DrawingSheetSizeEnum.kA4DrawingSheetSize:
                        Part.Drawing.Size = "A4";

 

    public class InventorDataService
    {

        public Inventor.Application invApp { get; set; }
        public Inventor.Document oInvDoc { get; set; }
        public PropertySets invAppPropertySet { get; set; }

        public void GetInventorApp()
        {

            invApp = (Inventor.Application)Marshal_NET6.GetActiveObject("Inventor.Application");
            oInvDoc = invApp.ActiveDocument;
            invAppPropertySet = invApp.ActiveDocument.PropertySets;
        }

    }

 

 

0 Likes
Message 6 of 9

WCrihfield
Mentor
Mentor

Hi @mowag. I see that you have updated your iLogic rule, but it is still not right. In order to avoid the document type mismatch error, you need to check the DocumentType of the document you are planning on working with 'before' you set the value of your document variable. There are two ways of doing this. If you prefer working with the 'ThisApplication.ActiveDocument' reference to identify the document you are going to be working with, then you could use something like the following.

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
	MsgBox("A Drawing Document must be active for this rule to work. Exiting.", vbCritical, "iLogic")
	Exit Sub
End If
Dim odrawdoc As DrawingDocument = ThisApplication.ActiveDocument

You need to do it in this order to prevent that possible error. If the document obtained from ThisApplication.ActiveDocument is not a drawing, then setting that to a DrawingDocument Type variable will throw an error. Attempting to check DocumentType after the fact is basically useless, unless your initial variable is a more generic Type, like Document (instead of DrawingDocument), which will accept any document type. However, I have transitioned to using mostly 'ThisDoc.Document' in almost all of my iLogic rules, because it is more dynamic, and causes fewer errors. There are some situations where using ThisApplication.ActiveDocument will return the wrong (or an unexpected) document (Link). With that in mind, I would recommend that you use something more like the following to start your rule:

If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
	MsgBox("A Drawing document must be active for this code to work. Exiting.", vbCritical, "")
	Exit Sub
End If
Dim oDDoc As DrawingDocument = ThisDoc.Document

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 9

mowag
Enthusiast
Enthusiast

Hi WCrihfield,

 

I've altered the Ilogic rule :

Screenshot_Scale5.png

 

Executing the .Save() gives me the MessageBox

 oDrawDoc.Save();

 

Screenshot_Scale6.png

Afther clicking the OK button i get again the error : 

 

Screenshot_Scale7.png

 

Is there still something else i can try ?

 

 

0 Likes
Message 8 of 9

WCrihfield
Mentor
Mentor
Accepted solution

😣 Now you are using 'ThisDoc.Document' to check DocumentType, but then using 'ThisApplication.ActiveDocument' to set the value of the DrawingDocument Type variable, which basically voided the check process.  As I said, the two can represent completely different documents in some situations.  You should use 'ThisDoc .Document' in both places, to avoid that potential problem.  Or, if used outside of iLogic, since the term 'ThisDoc' is not available, you might be forced to use the term 'ThisApplication.ActiveDocument' in both places, but as we have noticed here, using that term may not work for this situation.  There are other ways to refer to a specific document that you want a code to focus on outside of iLogic.  Let me try to explain how the 'active' document may be the wrong one.  When you are working with either an assembly, or a drawing, both can be referencing many other documents, and all of the documents that are being referenced within those documents will automatically be opened (not visibly, but in the background, which loads them into session memory).  If that assembly is the one currently visible on your screen when you start an iLogic rule, or VBA macro, or other form of code, then by default, that assembly will remain the 'active' document throughout the course of what ever else may be happening while those codes are running, even if your code opens many other documents and works with them, and even if other iLogic codes are triggered to run by anything the main code does while it is running.  So, none of those other documents will be the 'active' document, so if any other codes that get triggered to run are using the ThisApplication.ActiveDocument term to identify the document that they are to work on, they will essentially be targeting the main assembly, instead of the document they would normally be targeting if they were ran directly, with no other documents open.  The same thing goes for if you are currently editing a component within an assembly, the 'active' document is still going to be the main assembly, not that component's document.  Did you follow the link I provided in Message 6 above, and read through that article?  That should help explain some of the document references better, to help you overcome some of these types of problems.

Since you never posted the text of your iLogic rule, just images of it, I had to re-create all of the code from scratch, in an attempt to provide you with an example that you can try out.  I eliminated the MsgBox though, because it was not accomplishing anything anymore.

If ThisDoc.Document.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
	Dim oDDoc As DrawingDocument = ThisDoc.Document
	oCProps = oDDoc.PropertySets.Item("Inventor User Defined Properties")
	For i = 1 To oDDoc.Sheets.Count
		Dim oCProp As Inventor.Property = Nothing 'resets it
		Try
			oCProp = oCProps.Item("Scale" & i)
		Catch
			oCProp = oCProps.Add("", "Scale" & i)
		End Try
		Try
			oCProp.Value = oDDoc.Sheets.Item(i).DrawingViews.Item(1).ScaleString
		Catch
		End Try
	Next 'i
End If

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 9 of 9

mowag
Enthusiast
Enthusiast

Hi Wesley,

 

Thanks for the detailed explanation !!

To be honest, i didn't read then article to the end ! I stopped at VBA macro and VBA Applictation project!

Yesterday i read the entire article.

An yes, i didn't think of copying the Ilogic rul code(I'm relatively new to this forum).

Again, thanks for the help and this knowlledge will indeed help prevent errors in the future !!!!

 

Johan

0 Likes