.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Insert block with attributes, c sharp.

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
Anonymous
807 Views, 4 Replies

Insert block with attributes, c sharp.

Good Day,

 

I'm crafting a csharp programme through visual studio to calculate invert levels in autocad and insert a block with that level automatically added. Then repeat the calcs and insertion at any point the user clicks on until the programme is cancelled.

 

The calculations are complete but I'm having problems inserting the block. I'm trying to pull the block (Level_Arrow) from another drawing (C:\Test.dwg) to be placed at the target point (ptInsert) with block scale (drgScale), rotation 0 and (ptDisp) as first attribute, " " as second attribute (or nothing).

 

Is there a simple and easy way of doing this? 

 

Please note, I have experience with Lisp but little experience with Csharp. If your answer could be simple and well explained I would be grateful.

 

Thank you,

4 REPLIES 4
Message 2 of 5
BlackBox_
in reply to: Anonymous


@Anonymous wrote:

Good Day,

 

I'm crafting a csharp programme through visual studio to calculate invert levels in autocad and insert a block with that level automatically added. Then repeat the calcs and insertion at any point the user clicks on until the programme is cancelled.

 

The calculations are complete but I'm having problems inserting the block. I'm trying to pull the block (Level_Arrow) from another drawing (C:\Test.dwg) to be placed at the target point (ptInsert) with block scale (drgScale), rotation 0 and (ptDisp) as first attribute, " " as second attribute (or nothing).

 

Is there a simple and easy way of doing this? 

 

Please note, I have experience with Lisp but little experience with Csharp. If your answer could be simple and well explained I would be grateful.

 

Thank you,


Here's a snippet from a project that is copying attribute values from one block to another; complete Transaction not shown here:

 

				// ...

				    Dictionary<string, string> d = new Dictionary<string, string>();
                                    AttributeCollection ac = block.AttributeCollection;

                                    foreach (ObjectId attId in ac)
                                    {
                                        AttributeReference attRef =
                                            (AttributeReference)tr.GetObject(attId, OpenMode.ForRead);

                                        if (!d.ContainsKey(attRef.Tag))
                                        {
                                            d.Add(attRef.Tag, attRef.TextString);
                                        }
                                    }
									
                                    // <-- add your new BlockReference() here

                                    foreach (ObjectId x in <YourNewBlockHere>)
                                    {
                                        AttributeDefinition ad = 
                                            tr.GetObject(x, OpenMode.ForRead) as AttributeDefinition;

                                        if (ad != null)
                                        {
                                            AttributeReference ar = new AttributeReference();
                                            ar.SetAttributeFromBlock(ad, t);
                                            ar.Position = ad.Position.TransformBy(t);
                                            ar.TextString = d[ar.Tag];
                                        }
                                    }
									
				// ...

 

 

Cheers



"How we think determines what we do, and what we do determines what we get."

Message 3 of 5
bherber
in reply to: BlackBox_

i would also love a clean example,  been working\failing on trying to get the attributes to show up.

Message 4 of 5
Anonymous
in reply to: bherber

 
Message 5 of 5
Anonymous
in reply to: bherber

It took me days to figure out but I managed to extract the attributes. See extract: if (OrgBtr.HasAttributeDefinitions) { foreach (ObjectId id in OrgBtr) { DBObject obj = tr.GetObject(id, OpenMode.ForRead); AttributeDefinition ad = obj as AttributeDefinition; if (ad != null && !ad.Constant) { ar = new AttributeReference(); ar.SetAttributeFromBlock(ad, insblkref.BlockTransform); ar.Position = ad.Position.TransformBy(insblkref.BlockTransform); if (ad.Justify != AttachmentPoint.BaseLeft) { ar.AlignmentPoint.TransformBy(insblkref.BlockTransform); } if (ar.IsMTextAttribute) { ar.UpdateMTextAttribute(); } ar.TextString = ptDisp.ToString(("N3")); ObjectId arId = insblkref.AttributeCollection.AppendAttribute(ar); tr.AddNewlyCreatedDBObject(ar, true); break; } } } My work computer appears to have issue with forum posting setting I do hope this comes out legible.

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost