Inventor 2013 Add In code written in C#
I have managed to extract the "new" TitleBlockDefinition from my source drawing. (tblock)
I am trying to replace the title block definition in my active drawing with the "new" one.
Note. Both definitions have the same name, the only difference is that the logo has changed.
// I tried
DrawingDocument curDwg = (DrawingDocument)InvApp.ActiveDocument;
TitleBlockDefinition updatedTblock = tblock.CopyTo(curDwg, true);
This fails to compile because for some reason the CopyTo function expects a "_DrawingDocument" reference instead of the expected "DrawingDocument".
So I redefine curDwg to a _DrawingDocument and try again. This time the code compiles but unfortunately produces an error. Worse yet the error is vague.
System.Runtime.InteropServices.COMException (0x80004005): Unspecified error
(Exception from HRESULT:00x80004005 (E_FAIL))
Any ideas? The code for replacing a title block is in the Inventor API help file, however in VB not C#.
Solved! Go to Solution.
Solved by desidubajic. Go to Solution.
Hello
Is there a titleblock on one of the drawingsheets basing on the titleblockdefinition you try to substitute? You try this with Inventor dwg, not IDW right? Maybe that's a difference.
I am using Visual Studio 2010 professional.
The titleblock in the target drawing (.dwg) is the same name as the titleblock I am replacing it with.
They are identical, field by field. The only difference is that our logo has changed.
The problem appears to be C# specific.
In the sample code (Which is in Visual Basic) it seems pretty straight forward.
(SourcetitleBlockDefinition).CopyTo(TargetDocument, ReplaceExisting)
TargetDocument is a "DrawingDocument" object, and the second optional parameter is a Boolean indicating whether you want to overwrite "replace" an existing titleblockdefinition (I assume it should have the same name)
However attempting to use the same method in C# seems to be different.
TargetDocument is defined as an "Inventor._DrawingDocument". Note the leading underscore. Since I linked to the current document as a "Inventor.DrawingDocument" I can't even compile the code, since the two definitions are not identical.
If I link to the current document definining it as an "Inventor._DrawingDocument" then the code compiles however it generates the error (I mentioned in my previous post) when executed. I'm a little stymied as to what to do next.
We are using Inventor 2013 with Vault Professional 2013
We have thousands of drawings, and many are "released" and locked away. I cannot batch process the drawings as changes would require updating released and revisioned drawings.
As we update assemblies, we modify any drawings we come across that have the old logo. It's not really a biig deal to do the change manually, I was just hoping to provide a simple one touch update.
I've rewritten the code in Visual Basic, however I am having the same error occur. In the sample it is updating an .idw file, and I am using .dwg files. Surely this isn't an issue.
Unspecified error (Exceptoin from HRESULT: 0x80004005 (E_FAIL)
This is such a simple thing, yet very frustrating. I'm just not sure what to do. (I guess abandon the idea, as I am wasting too much time on this)
I've resolved the issue. The mistake I made was prematurely closing the drawing that contained the source template. My C# code now works.
What made it frustrating, was due to the vague error code I focused on the wrong area. The one thing that threw me was that my dwg variable was a DrawingDocument, yet the CopyTo method required a "_DrawingDocument". In the end it was a simple matter of "NewTBlock = SrcTBlock.CopyTo((_DrawingDocument)CurDwg, True).
I had tried that before, but it didn't work of course because the src drawing was already closed pror to callling this function.
sigh .. oh well ... next hurdle.
Thanks to everyone who responded.