Insert block with attributes, c sharp.

Insert block with attributes, c sharp.

Anonymous
Not applicable
1,017 Views
4 Replies
Message 1 of 5

Insert block with attributes, c sharp.

Anonymous
Not applicable

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,

0 Likes
Accepted solutions (1)
1,018 Views
4 Replies
Replies (4)
Message 2 of 5

BlackBox_
Advisor
Advisor
Accepted solution

@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."

Sincpac C3D ~ Autodesk Exchange Apps

0 Likes
Message 3 of 5

bherber
Contributor
Contributor

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

0 Likes
Message 4 of 5

Anonymous
Not applicable
 
0 Likes
Message 5 of 5

Anonymous
Not applicable
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.
0 Likes