• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    *Expert Elite*
    Posts: 6,474
    Registered: ‎06-29-2007

    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
    -------------------------------------------------------------------------
    Please use plain text.
    Active Contributor
    Posts: 26
    Registered: ‎03-10-2012

    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:

    Please use plain text.
    *Expert Elite*
    Posts: 6,474
    Registered: ‎06-29-2007

    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
    -------------------------------------------------------------------------
    Please use plain text.
    Active Contributor
    Posts: 26
    Registered: ‎03-10-2012

    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?

    Please use plain text.
    *Expert Elite*
    Posts: 6,474
    Registered: ‎06-29-2007

    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
    -------------------------------------------------------------------------
    Please use plain text.
    Active Contributor
    Posts: 26
    Registered: ‎03-10-2012

    Re: Edit a Block via C#

    03-11-2012 04:34 AM in reply to: alfred.neswadba

    Oh.. they are very simple! :smileylol:

    Please use plain text.
    *Expert Elite*
    Posts: 6,474
    Registered: ‎06-29-2007

    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
    -------------------------------------------------------------------------
    Please use plain text.
    Active Contributor
    Posts: 26
    Registered: ‎03-10-2012

    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:

    Please use plain text.
    *Expert Elite*
    Posts: 6,474
    Registered: ‎06-29-2007

    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
    -------------------------------------------------------------------------
    Please use plain text.
    Active Contributor
    Posts: 26
    Registered: ‎03-10-2012

    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?

    Please use plain text.