Author Topic: Block Attributed ( HELP )  (Read 4752 times)

0 Members and 1 Guest are viewing this topic.

josano

  • Guest
Block Attributed ( HELP )
« on: October 06, 2010, 01:48:29 PM »
Friends,

Searching the forums, I could ff-figure more or less works as part of the block attribute.
The code below changes the attribute value in this block.
I'm trying to get the code to recognize more than one attribute, in case 4 but not very good at c # I'm not getting.

Anyone have an idea of how do I change the value of more than one attribute with this code.

Thanks,
Josano
Code: [Select]
using System;
using System.Collections.Generic;
using System.Text;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.ApplicationServices;
using acad = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.EditorInput;

namespace ClassLibrary5
{
    public class Class1
    {
        [CommandMethod("TESTE")]
        public static void UpdateSingleAttribute()
        {
            Database db = HostApplicationServices.WorkingDatabase;

            Editor ed = acad.DocumentManager.MdiActiveDocument.Editor;

            Transaction tr = db.TransactionManager.StartTransaction();

            using (tr)
            {

                try
                {

                    PromptNestedEntityOptions pno = new PromptNestedEntityOptions("\nSelecione o Bloco Atributado >>");

                    PromptNestedEntityResult nres = ed.GetNestedEntity(pno);

                    if (nres.Status != PromptStatus.OK) return;

                    ObjectId id = nres.ObjectId;

                    Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead);

                    ed.WriteMessage("\nObeto Selecionado\t{0}", ent.GetRXClass().DxfName);

                    Point3d pnt = nres.PickedPoint;

                    AttributeReference attref = ent as AttributeReference;


                        if (attref == null) return;

                        PromptStringOptions pso =
                                        new PromptStringOptions("\nDigite novo valor para ITEM: ");

                        pso.AllowSpaces = true;

                        PromptResult res;

                        res = ed.GetString(pso);

                        if (res.Status != PromptStatus.OK) return;

                        string strvalue = res.StringResult;

                        attref.UpgradeOpen();

                        attref.TextString = strvalue;
                

                        tr.Commit();
                    }
                catch (Autodesk.AutoCAD.Runtime.Exception ex)
                {

                    ed.WriteMessage("\n" + ex.StackTrace + "\n" + ex.Message);
                }
                finally
                {

                }
            }
        }
    }
}
Edit: Cmdrduh - Added code tags

« Last Edit: October 06, 2010, 06:37:55 PM by CmdrDuh »

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Block Attributed ( HELP )
« Reply #1 on: October 06, 2010, 06:45:46 PM »
I am not sure if your asking to change all the attributes to the same value or how you plan on what gets changed to what, but
this is your code modified a little to give an idea how to to keep selecting and changing attributes until the user escapes.
 
Code: [Select]
[CommandMethod("TESTE")]
        public static void UpdateSingleAttribute()
        {
            Database db = HostApplicationServices.WorkingDatabase;

            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

            Transaction tr = db.TransactionManager.StartTransaction();
           
            using (tr)
            {

                try
                {
                    Entity ent;
                    PromptNestedEntityResult nres;
                    ObjectId id;
                    do
                    {
                        PromptNestedEntityOptions pno = new PromptNestedEntityOptions("\nSelecione o Bloco Atributado >>");
                        nres = ed.GetNestedEntity(pno);

                        if (nres.Status != PromptStatus.OK)
                        {
                            break;
                        }
                         id = nres.ObjectId;                                             
                         
                         ent = id.GetObject(OpenMode.ForRead) as Entity;                 
                                           

                        ed.WriteMessage("\nObeto Selecionado\t{0}", ent.GetRXClass().DxfName);

                        Point3d pnt = nres.PickedPoint;

                        AttributeReference attref = ent as AttributeReference;


                        if (attref == null) break;

                        PromptStringOptions pso =
                                        new PromptStringOptions("\nDigite novo valor para ITEM: ");

                        pso.AllowSpaces = true;

                        PromptResult res;

                        res = ed.GetString(pso);

                        if (res.Status != PromptStatus.OK) break;

                        string strvalue = res.StringResult;

                        attref.UpgradeOpen();

                        attref.TextString = strvalue;                   
                    }
                    while (nres.Status == PromptStatus.OK);
                    tr.Commit();
                }
                 
                catch (Autodesk.AutoCAD.Runtime.Exception ex)
                {

                    ed.WriteMessage("\n" + ex.StackTrace + "\n" + ex.Message);
                }
               
            }
        }

josano

  • Guest
Re: Block Attributed ( HELP )
« Reply #2 on: October 08, 2010, 10:19:00 AM »
Friends,

What I'm trying to do is access the tag and attribute prompt in this block.
My block has one attribute visible and two invisible .
I do not understand how to access attributes that are invisible to modify them.
I have attached the block, if any can help I am extremely grateful.
Not yet found a way to access, does anyone know where I am wrong in above code?

Thank you,

kaefer

  • Guest
Re: Block Attributed ( HELP )
« Reply #3 on: October 08, 2010, 11:31:00 AM »
What I'm trying to do is access the tag and attribute prompt in this block.

Please confirm: AttributeReference.Tag? AttributeDefinition.Prompt?

Quote
My block has one attribute visible and two invisible .

I do not understand how to access attributes that are invisible to modify them.

You would have to look them up via the BlockReference.AttributeCollection mechanism.
Code: [Select]
                    // Assuming ent is the selected (visible) AttributeReference
                    BlockReference blref = tr.GetObject(ent.OwnerId, OpenMode.ForRead) as BlockReference;
                    foreach (ObjectId attId in blref.AttributeCollection) {
                        AttributeReference attref = tr.GetObject(attId, OpenMode.ForRead) as AttributeReference;
                        if (attref.Tag == "MyInvisibleAttributeTag") {
                            // ...
                            break;
                        }
                    }
Have fun, Thorsten



josano

  • Guest
Re: Block Attributed ( HELP )
« Reply #4 on: October 08, 2010, 03:37:01 PM »
kaefer,

It's prompt attribute, wanted him to appear at the prompt cad not to have to choose how I hj as attributes that are invisible can not be selected.
The block attachment is thus a visible and two invisible, I tried it but did not notice a difference in the invisible.
Modified with the tips you gave me but still could not, I apologize in advance to everyone I'm still crawling in C # programming and cad.

using System;
using System.Collections.Generic;
using System.Text;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.ApplicationServices;
using acad = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.EditorInput;

namespace ClassLibrary5
{
    public class Class1
    {
        [CommandMethod("TESTE")]
        public static void UpdateSingleAttribute()
        {
            Database db = HostApplicationServices.WorkingDatabase;

            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

            Transaction tr = db.TransactionManager.StartTransaction();

            using (tr)
            {

                try
                {
                    Entity ent;
                    PromptNestedEntityResult nres;
                    ObjectId id;

                    do
                    {
                        PromptNestedEntityOptions pno = new PromptNestedEntityOptions("\nSelecione o Bloco Atributado >>");
                        nres = ed.GetNestedEntity(pno);

                        if (nres.Status != PromptStatus.OK)
                        {
                            break;
                        }
                        id = nres.ObjectId;

                        ent = id.GetObject(OpenMode.ForRead) as Entity;


                        ed.WriteMessage("\nObjeto Selecionado\t{0}", ent.GetRXClass().DxfName);

                        Point3d pnt = nres.PickedPoint;
                        AttributeReference attref = tr.GetObject(id, OpenMode.ForRead) as AttributeReference;
                        AttributeDefinition msg = ent as AttributeDefinition;
                        BlockReference blref = tr.GetObject(ent.OwnerId, OpenMode.ForRead) as BlockReference;
                        foreach (ObjectId attId in blref.AttributeCollection)
                        {
                           
                            if (attref.Tag == "MyInvisibleAttributeTag")
                            {
                                PromptStringOptions pso = new PromptStringOptions("\nDigite novo valor : ");

                                pso.AllowSpaces = true;

                                PromptResult res;

                                res = ed.GetString(pso);
                                break;
                            }
                        }
                     
                        PromptStringOptions psso = new PromptStringOptions("\nDigite novo valor : ");

                        psso.AllowSpaces = true;

                        PromptResult rres;

                        rres = ed.GetString(psso);

                        if (rres.Status != PromptStatus.OK) break;

                        string strvalue = rres.StringResult;

                        attref.UpgradeOpen();

                        attref.TextString = strvalue;
                    }
                    while (nres.Status == PromptStatus.OK);
                    tr.Commit();
                }

                catch (Autodesk.AutoCAD.Runtime.Exception ex)
                {

                    ed.WriteMessage("\n" + ex.StackTrace + "\n" + ex.Message);
                }

            }
        }
    }
}

Thanks,
Josano

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Block Attributed ( HELP )
« Reply #5 on: October 08, 2010, 03:53:16 PM »
Are you wanting to select the block and then be able to change all the attributes visible or invisible?

josano

  • Guest
Re: Block Attributed ( HELP )
« Reply #6 on: October 11, 2010, 10:33:22 AM »
Exactly

I'm not able to select the block and be able to change the attributes invisible. The visible I can because I can select them, the invisible I can not.
Any idea how to do this?
Thanks,
Josano

kaefer

  • Guest
Re: Block Attributed ( HELP )
« Reply #7 on: October 11, 2010, 11:29:26 AM »
I'm not able to select the block and be able to change the attributes invisible. The visible I can because I can select them, the invisible I can not.
Any idea how to do this?

You would have to look them up via the BlockReference.AttributeCollection mechanism.

Since you are very economic with the information required to help to you, I will assume that you do not know in advance the tags of the invisible attributes in question. My proposal is that you loop through the AttributeCollection and store all invisible attributes in a dictionary, which you can afterwards consume at leisure.

Untested code fragment ahead:
Code: [Select]
                       BlockReference blref = tr.GetObject(ent.OwnerId, OpenMode.ForRead) as BlockReference;
                        Dictionary<String, AttributeReference> dict = new Dictionary<string, AttributeReference>();
                        foreach (ObjectId attId in blref.AttributeCollection)
                        {
                            AttributeReference attref = tr.GetObject(attId, OpenMode.ForRead) as AttributeReference;
                            if (attref.Invisible)
                                dict.Add(attref.Tag, attref);
                        }
                        PromptResult res;
                        foreach (KeyValuePair<String,AttributeReference> entry in dict)
                        {
                            PromptStringOptions pso =
                                    new PromptStringOptions(
                                            String.Format("\nDigite novo valor para \"{0}\": ", entry.Key));
                            pso.AllowSpaces = true;
                            res = ed.GetString(pso);
                            if (res.Status != PromptStatus.OK)
                                break;
                            entry.Value.UpgradeOpen();
                            entry.Value.TextString = res.StringResult;
                        }

Cheers, Thorsten
« Last Edit: October 11, 2010, 12:07:32 PM by kaefer »

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Block Attributed ( HELP )
« Reply #8 on: October 12, 2010, 06:09:28 PM »
Exactly

I'm not able to select the block and be able to change the attributes invisible. The visible I can because I can select them, the invisible I can not.
Any idea how to do this?
Thanks,
Josano

If you would rather select the block then
do not use PromptNestedEntityResult
You could use PromptEntityResult to select the block then run a loop to grab the attributes.
Reply back if you need more help

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Block Attributed ( HELP )
« Reply #9 on: October 12, 2010, 09:37:16 PM »
Here is your code modified a little that will allow you to select a block
Then will loop through each attribute visible or invisible and print to the command line "Change <AttributeName>:"
If you presss enter or a empty string It will not change the value and go to the next. If you enter a value then it will update the value.

It will update on the screen the new values once you change all the attributes of the block
If you want it to update after you change each attribute value then uncomment the first db.TransactionManager.QueueForGraphicsFlush(); and comment out the last.


Code: [Select]

[CommandMethod("TESTF")]
        public static void UpdateMultipleAttribute()
        {

            Database db = HostApplicationServices.WorkingDatabase;

            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

            Transaction tr = db.TransactionManager.StartTransaction();

            using (tr)
            {

                try
                {
                    Entity ent;
                    PromptEntityResult per;
                    ObjectId id;

                    do
                    {
                        PromptEntityOptions pno = new PromptEntityOptions("\nSelecione o Bloco Atributado >>");
                        per = ed.GetEntity(pno);

                        if (per.Status != PromptStatus.OK)
                        {
                            break;
                        }
                        id = per.ObjectId;

                        ent = id.GetObject(OpenMode.ForRead) as Entity;


                        ed.WriteMessage("\nObjeto Selecionado\t{0}", ent.GetRXClass().DxfName);

                        if (!(ent is BlockReference))
                        {
                            ed.WriteMessage("\nNot a Block Reference");
                            continue;
                        }
                        BlockReference blref = ent as BlockReference;
                        foreach (ObjectId attId in blref.AttributeCollection)
                        {
                            AttributeReference attref = attId.GetObject(OpenMode.ForRead) as AttributeReference;
                           
                            PromptStringOptions psso = new PromptStringOptions("\nChange <" + attref.Tag + ">: ");

                            psso.AllowSpaces = true;

                            PromptResult rres;

                            rres = ed.GetString(psso);

                            if (rres.Status != PromptStatus.OK) break;

                            string strvalue = rres.StringResult;

                            if (strvalue == "")
                            {
                                continue;
                            }
                            attref.UpgradeOpen();

                            attref.TextString = strvalue;
                            // Uncomment line below to update screen after each attribute is changed
                            //db.TransactionManager.QueueForGraphicsFlush();
                        }
                        db.TransactionManager.QueueForGraphicsFlush();
                    }
                    while (per.Status == PromptStatus.OK);
                    tr.Commit();
                }

                catch (Autodesk.AutoCAD.Runtime.Exception ex)
                {

                    ed.WriteMessage("\n" + ex.StackTrace + "\n" + ex.Message);
                }

            }
        }

josano

  • Guest
Re: Block Attributed ( HELP )
« Reply #10 on: October 13, 2010, 10:28:43 AM »
Jeff H and Kaefer

Thanks for the help.
That's what I did not understand how to modify.
I will now move on to the second part :)

Thank you,
Josano