.NET

Reply
*Expert Elite*
Alfred.NESWADBA
Posts: 8,892
Registered: ‎06-29-2007
Message 11 of 29 (314 Views)

Re: Edit a Block via C#

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

Hi,

 

 

 

>> something is wrong with the last if (tObjID.ObjectClass... and so on). 

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

What AutoCAD-version do you have? And have you set LateBinding as allowed in your C#-project-settings?

The .DxfName did not exist from the beginning of AutoCAD & dotNET, it was added with AutoCAD 2010 I think (but I'm not sure about that, maybe a version earlier).

Anyway, if the property would not exist in your AutoCAD-version you should get an exception then ... do you see such when debugging?

 

>> listBlockAttributes

What type is this variable?

 

- alfred -

 

PS: "bin im using" deutet auf D oder A oder CH hin???

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

Re: Edit a Block via C#

03-10-2012 06:39 AM in reply to: Alfred.NESWADBA

Autocad-version 2012

Visual Studio 2010

 

Latebinding doesn't mean anything to me. :smileyindifferent:

 

And at debugging, i get no exception(?).

 

listBlockAttributes is a ListBox.

 

PS: Österreich :smileywink:

*Expert Elite*
Alfred.NESWADBA
Posts: 8,892
Registered: ‎06-29-2007
Message 13 of 29 (307 Views)

Re: Edit a Block via C#

03-10-2012 07:14 AM in reply to: klahie

Hi,

 

without error-handling (and some checks like check for proxy, for xref, ...) this works with my drawings (having blocks with attributes) and AutoCAD 2010, 2011, 2012, 32bit and 64bit:

 

        [Autodesk.AutoCAD.Runtime.CommandMethod("ADESK_ListBlockAttdefs")]
        public void ADESK_ListBlockAttdefs()
        {
            string ResultStr = "";

            Document tAcadDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            using (Transaction tTrAct = tAcadDoc.TransactionManager.StartTransaction())
            {
                BlockTable tBlTab = tTrAct.GetObject(tAcadDoc.Database.BlockTableId, OpenMode.ForRead) as BlockTable;
                IEnumerator tBlTabEnum = tBlTab.GetEnumerator();
                while (tBlTabEnum.MoveNext())
                {
                    BlockTableRecord tBlTabRec =tTrAct.GetObject((ObjectId) tBlTabEnum.Current ,OpenMode.ForRead) as BlockTableRecord;
                    IEnumerator tBlTabRecEnum = tBlTabRec.GetEnumerator();
                    while (tBlTabRecEnum.MoveNext())
                    {
                        ObjectId tObjID = (ObjectId)tBlTabRecEnum.Current;
                        if ((tObjID.IsValid == true) && (tObjID.IsErased == false) && (tObjID.ObjectClass.DxfName.ToUpper() == "ATTDEF"))
                        {
                            AttributeDefinition tAttDef = tTrAct.GetObject(tObjID,OpenMode.ForRead) as AttributeDefinition;
                            ResultStr += "Block: " + tBlTabRec.Name + "... Att: " + tAttDef.Tag +  "\n\r";
                        }
                    }
                }
            }
            //here you have the result of all your Blockdefinitions and their Attributedefinitions
        }

 

 

- alfred -

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

Re: Edit a Block via C#

03-11-2012 04:30 AM in reply to: Alfred.NESWADBA

Okay, i've tried your solutioin a bit, but the compiler isn't able to get in this third if:

 

if(tObjID.ObjectClass.DxfName.ToUpper() == "ATTDEF")
{
    ...
}

 The value of tObjID.ObjectClass.DxfName.ToUpper() is always something like "LINE", "LWPOLYLINE", etc. but never "ATTDEF". 

Hmmm, and now?

*Expert Elite*
Alfred.NESWADBA
Posts: 8,892
Registered: ‎06-29-2007
Message 15 of 29 (292 Views)

Re: Edit a Block via C#

03-11-2012 04:31 AM in reply to: klahie

Hi,

 

can you upload a drawing that you work on for testing the code?

 

- alfred -

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

Re: Edit a Block via C#

03-11-2012 04:34 AM in reply to: Alfred.NESWADBA

Oh.. they are very simple! :smileylol:

*Expert Elite*
Alfred.NESWADBA
Posts: 8,892
Registered: ‎06-29-2007
Message 17 of 29 (288 Views)

Re: Edit a Block via C#

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

Hi,

 

you don't have Attribtutes (not -definitions and no -references) in your drawing. So the scan will never find one as there are none. :smileywink:

Try my attachment, a traffic-sign with a lot of attributes.

 

- alfred -

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

Re: Edit a Block via C#

03-11-2012 04:51 AM in reply to: Alfred.NESWADBA

Thanks for that, my Autocad-experiences belong to a 15min introduction. :smileywink:

 

Okay, I've tried it and now I see a lot more than 10 minutes ago! :smileyhappy:

 

Next step: editting these attributes... We will see... :smileywink:

*Expert Elite*
Alfred.NESWADBA
Posts: 8,892
Registered: ‎06-29-2007
Message 19 of 29 (283 Views)

Re: Edit a Block via C#

03-11-2012 04:59 AM in reply to: klahie

Hi,

 

>> Next step: editting these attributes

Be careful, you should understand some AutoCAD-internals to be able to develop based on it.

 

In this example you have to differenciate between AttributeDefinition and AttributeReference! If you want to modify the definition of the block, then you have to do this with BlockTableRecord and AttribtueDefinition, if you want to modify the speed-limit of my traffic-sign you have to use BlockReference and AttributeCollection and AttributeReference.


The example-code above handles the definitions.

 

- alfred -

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

Re: Edit a Block via C#

03-11-2012 05:06 AM in reply to: Alfred.NESWADBA

It should be generally able to edit these attributes:

 

- Layer

- Insertionpoint

- Basepoint

- Rotation

- Scaling X

- Scaling Y

- Scaling Z

- Sampling fraction (Auswahlsatz in Englisch? :smileywink: )

 

But this is the next problem, I see many attributes like kmh, beleuchtet, mast_id, etc. but non of those, I've mentioned.

How can i reach them?

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community