How to insert dwg autocadblock on Inventor drawing using c#.net addins

How to insert dwg autocadblock on Inventor drawing using c#.net addins

Anonymous
Not applicable
1,457 Views
6 Replies
Message 1 of 7

How to insert dwg autocadblock on Inventor drawing using c#.net addins

Anonymous
Not applicable

Hello,

I have to insert dwg file from autocad blocks into specific location on inventor template drawing.
Also i have insert dwg file from secifiled folder on inebentor drawing on specific part using c#.net add inson click of button of WPF window.

I have write some code for it but did not work.

 

public Inventor.AutoCADBlock ReplaceOrInsertTitleBlock(DrawingDocument doc, string tbName, string fromIDWFile)
{
DrawingDocument from = m_inventorApplicationNew.Documents.Open(fromIDWFile, false) as DrawingDocument;
int count = from.AutoCADBlockDefinitions.Count;
AutoCADBlockDefinition tbdToInsert = from.AutoCADBlockDefinitions[tbName];
AutoCADBlockDefinition tbdCopied = tbdToInsert.CopyTo(doc as _DrawingDocument, true);
Point2d point2 = null;
point2.X = 0.00;
point2.Y = 0.00;
AutoCADBlock tb = doc.ActiveSheet.AutoCADBlocks.Add(tbdCopied, point2, 0,1,"Hello");

from.Close(true);

return tb;
}
public void TestInsertTitleBlock()
{
DrawingDocument drawDoc = (DrawingDocument)m_inventorApplicationNew.ActiveDocument;
ReplaceOrInsertTitleBlock(drawDoc, "DWG_Stamp_AsBuiltRevised", @"C:\CAD Support\2019\Blocks\DWG_Stamp_AsBuiltRevised.dwg");
}


private void button_Ok_Click(object sender, RoutedEventArgs e)
{
}

 

 

I have shown in screen show  (attachment) what which is now we are manually doing we want it on click of button of wpf.

 

 

0 Likes
Accepted solutions (2)
1,458 Views
6 Replies
Replies (6)
Message 2 of 7

JamieVJohnson2
Collaborator
Collaborator

What error or symptom of failure are you getting?

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
0 Likes
Message 3 of 7

Anonymous
Not applicable

I have attached error that  i have getting...

Please provide alternative code for it if you know....

0 Likes
Message 4 of 7

JamieVJohnson2
Collaborator
Collaborator
Accepted solution

"The parameter is incorrect" means you are asking Inventor do so something with bogus input. Such as getting a block definition by name (can't see the entire code line), and supplying a name in the string variable for a block definition that is not in the CAD file.

Alternate method, would be to check to see if the block definition exists in the file first, then get it (vb.net syntax).

            For Each bd As AutoCADBlockDefinition In from.AutoCADBlockDefinitions
                If bd.Name = tbName Then
                    'now get the block
                End If
            Next
Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
0 Likes
Message 5 of 7

Anonymous
Not applicable

Thanks for the solution...

 One more question...

 

We want to edit AutoCADBlock attribute values as we want to add some text in the block.

 

we have try to set value for the attribute but it shows invalid parameter exception ...

I have added the screenshot for what exactly we want.

 

Following is code for the same:

 

foreach(AutoCADBlockDefinition bd in from.AutoCADBlockDefinitions)
{
if(bd.Name == tbName)
{
AutoCADBlockDefinition tbdToInsert = from.AutoCADBlockDefinitions[tbName];
AutoCADBlockDefinition tbdCopied = tbdToInsert.CopyTo(doc as _DrawingDocument, true);
object a1 = new object();


Point2d oCenter = m_inventorApplicationNew.TransientGeometry.
CreatePoint2d(103.98819206772882, 27.184943343471083);
object PromptStrings = new object();

AutoCADBlock tb = doc.ActiveSheet.AutoCADBlocks.Add(tbdCopied, oCenter, 0, 1);
String promtname = "ASBUILTDATEANDTIME";
String promtValue = "25-Jan-419";
tb.SetPromptTextValues(promtname, promtValue);
// tb.SetPromptTextValues(a1, a2);
return tb;
}
}

 

0 Likes
Message 6 of 7

JamieVJohnson2
Collaborator
Collaborator

AutoCAD *.dwg Attributes are defined in the block (AttributeDefinition), but then inserted AFTER the block reference is placed (an association to the block reference of sorts).  This is a strange behavior to most, so you can't modify the attribute definition, and expect to get a unique value for the AttributeReference.  I am experienced with the AutoCAD managed .net api, but have not much had a chance to apply through the Inventor wrapper for AutoCAD.  Give me a bit of time, and I'll look into it, unless you manage to discover it for yourself by then.

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
0 Likes
Message 7 of 7

JamieVJohnson2
Collaborator
Collaborator
Accepted solution

Ok so I converted your code to my native vb.net language and immediately see the issue

.SetPromptTextValues(strArrayOfTags, strArrayOfValues)

it requires array values for each.  You started doing that, but then changed gears.

a1, and a2 as object() was creating new object arrays, which is legal, but what you really wanted was

string() array for each, then created 2 string variables, compile your thoughts and simplify, so try this:

{
    AutoCADBlockDefinition tb;
    string[] strArrayOfTags = new[] { "ASBUILTDATREANDTIME" };
    string[] strArrayOfValues = new[] { "25-Jan-419" }; // btw what's with the 419?
    tb.SetPromptTextValues(strArrayOfTags, strArrayOfValues);
//or this, the all in one line method) tb.SetPromptTextValues(new string[] { "ASBUILTDATREANDTIME" }, new string[] { "25-Jan-419" }); }

 

Also 419?  that is not a year in most pc systems, as Inventor only goes back to 1600.

 

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
0 Likes