.NET

Reply
Active Contributor
klahie
Posts: 26
Registered: ‎03-10-2012
Message 1 of 29 (1,424 Views)

Edit a Block via C#

1424 Views, 28 Replies
03-10-2012 04:15 AM

Hi community,

 

my job is to make an editor for blocks in C#. There should be a WPF-window were the user can see a list of blocks. He selects one and than he should see an overview about block attributes like insertionpoint, basepoint, rotation and scaling and he should be able to edit these attributes in this form. 

 

I know how i can populate a list of block names, but I fail at finding an opportunity to edit these attributes. 

I get the blocktablerecords, but then?

 

I am very, very new to Autocad, and I've googled the last five hours unsuccessfully for a solution which is suitable for my problem. 

 

Please help me, it is very urgent! 

 

Kind regards, 

Klaus

*Expert Elite*
Alfred.NESWADBA
Posts: 9,099
Registered: ‎06-29-2007
Message 2 of 29 (1,420 Views)

Re: Edit a Block via C#

03-10-2012 04:19 AM in reply to: klahie

Hi,

 

>> I get the blocktablerecords, but then?

Scan through the entities in the BlockTableRecord and find out which one is from type AttributeDefinition

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Active Contributor
klahie
Posts: 26
Registered: ‎03-10-2012
Message 3 of 29 (1,418 Views)

Re: Edit a Block via C#

03-10-2012 04:26 AM in reply to: Alfred.NESWADBA

The only thing i've found with "AttributeDefinition" was

 

blocktablerecord.HasAttributeDefinitions,

 

but nothing with type "AttributeDefinition". Maybe I've missunderstood you?

*Expert Elite*
Alfred.NESWADBA
Posts: 9,099
Registered: ‎06-29-2007
Message 4 of 29 (1,414 Views)

Re: Edit a Block via C#

03-10-2012 04:35 AM in reply to: klahie

Hi,

 

go through each DBObject in the BlockTableRecord

verify if the current checked object is an AttributeDefintion, if it is one you have one (of may be more) AttributeDefinitions, take them for your handling you need to do with them.

 

Dim tTrAct As DatabaseServices.Transaction = Nothing 'set your tTrAct
Dim tBlTabRec As DatabaseServices.BlockTableRecord = Nothing 'set your tBlTabRec

'now scan through the objects in the BTR
For Each tObjID As DatabaseServices.ObjectId In tBlTabRec
  If (tObjID.IsValid) AndAlso (Not tObjID.IsErased) AndAlso (tObjID.ObjectClass.DxfName.ToUpper = "ATTDEF") Then
    'ok object is valid and it's a AttributeDefinition
    Dim tAttDef As DatabaseServices.AttributeDefinition = CType(tTrAct.GetObject(tObjID, DatabaseServices.OpenMode.ForRead), DatabaseServices.AttributeDefinition)
    'here you have your access to the AttributeDefinition-object
  End If
Next

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Active Contributor
klahie
Posts: 26
Registered: ‎03-10-2012
Message 5 of 29 (1,413 Views)

Re: Edit a Block via C#

03-10-2012 04:40 AM in reply to: klahie

Okay, i try to translate this in C# and i hope I am successful with that. :smileywink:

 

*Expert Elite*
Alfred.NESWADBA
Posts: 9,099
Registered: ‎06-29-2007
Message 6 of 29 (1,410 Views)

Re: Edit a Block via C#

03-10-2012 04:54 AM in reply to: klahie

Hi,

 

>> i try to translate

If you don't know it: that may help >>>translate c# vb<<<

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Active Contributor
klahie
Posts: 26
Registered: ‎03-10-2012
Message 7 of 29 (1,404 Views)

Re: Edit a Block via C#

03-10-2012 05:29 AM in reply to: Alfred.NESWADBA

At 

if ((tObjID.IsValid) && (tObjID.IsErased == false) && (tObjID.ObjectClass.DxfName.ToUpper = "ATTDEF"))
{
     AttributeDefinition tAttDef = (AttributeDefinition)tTrAct.GetObject(tObjID, OpenMode.ForRead);
}

 

 I get an error:

 

Cannot assign to 'ToUpper' because it is a 'method group'.

Do you know a workaround?

*Expert Elite*
Alfred.NESWADBA
Posts: 9,099
Registered: ‎06-29-2007
Message 8 of 29 (1,399 Views)

Re: Edit a Block via C#

03-10-2012 05:37 AM in reply to: klahie

Hi,

 

you can erase the .ToUpper as it should not be necessary in that case, AutoCAD reports "ATTDEF" already in uppercase IMHO.

But I think you have to set double "=" in that statement for comparision in C#? So one equal-sign is missing and then .ToUpper should work also!

      tObjID.ObjectClass.DxfName.ToUpper == "ATTDEF"

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Active Contributor
klahie
Posts: 26
Registered: ‎03-10-2012
Message 9 of 29 (1,398 Views)

Re: Edit a Block via C#

03-10-2012 05:41 AM in reply to: Alfred.NESWADBA

Yes, the second "=" helped, but it needed .ToUpper() to really work. 

 

Okay I look how far i get with that and I will come back to this thread if I have any further problems. 

 

Thank you very much!

Active Contributor
klahie
Posts: 26
Registered: ‎03-10-2012
Message 10 of 29 (1,388 Views)

Re: Edit a Block via C#

03-10-2012 06:09 AM in reply to: klahie

Further problems! :smileysad:

 

I've tried to print out some attributes but nothing happened... Now I've debugged this code:

 

if ((tObjID.IsValid) && (tObjID.IsErased == false) && (tObjID.ObjectClass.DxfName.ToUpper() == "ATTDEF"))
{
     window.listBlockAttributes.Items.Add(tAttDef.Layer);
     window.listBlockAttributes.Items.Add(tAttDef.Rotation.ToString());
}

 and i realized that something is wrong with the last if (tObjID.ObjectClass... and so on). 

The Code passes the first two ifs but never the third one.

 

Can you help me again? :smileywink:

 

The whole code of this method:

 

[CommandMethod("loadbe")]
        public static void LoadBe()
        {
            Window1 window = new Window1();
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            
            Transaction tr = db.TransactionManager.StartTransaction();
            List<String> blocks = new List<string>();
            using (tr)
            {
                window.listBlockAttributes.Items.Add("bin im using");
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                foreach (ObjectId objId in bt)
                {
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(objId, OpenMode.ForRead);

                    foreach (ObjectId tObjID in btr)
                    {

                        if ((tObjID.IsValid) && (tObjID.IsErased == false) && (tObjID.ObjectClass.DxfName.ToUpper() == "ATTDEF"))
                        {
                            AttributeDefinition tAttDef = (AttributeDefinition)tr.GetObject(tObjID, OpenMode.ForRead);
                            window.listBlockAttributes.Items.Add(tAttDef.Layer);
                            window.listBlockAttributes.Items.Add(tAttDef.Rotation.ToString());
                        }
                    }
                    window.listBlocks.Items.Add(btr.Name);
                }
            }
            Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessWindow(window);
        }

 Yeah, i know this code is not very useful, but i only wanted to know if there are any attributes which i can print out and I throw them in a list for now.

 

Kind Regards

Klaus

Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.