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

    .NET

    Reply
    Active Contributor
    Posts: 37
    Registered: ‎02-14-2008

    Attribute to move to block without the old ATT

    350 Views, 35 Replies
    09-10-2012 05:27 AM

    Hello I can help defense.
    How can I add an attribute to a block without the other attributes to move from their position.
    With the ATTSYNC belongs unfortunately not because the old attributes moved back to the original places that I will not.
    Please help

     (Attribut zu Block ohne die alten ATT zu verschieben)

    Hallo kann mir wehr Helfen.
    Wie kann ich ein Attribut zu einem Block hinzufügen ohne die anderen Attribute von ihrer Position zu verschieben.
    Mit ATTSync gehrt das leider nicht da werden die alten Attribute wieder auf die Originalplätze verschoben das will ich aber nicht.
    Bitte um Hilfe

    DH
    Please use plain text.
    Active Contributor
    Posts: 37
    Registered: ‎02-14-2008

    Re: Attribute to move to block without the old ATT

    09-11-2012 09:54 PM in reply to: heinz.dober

    Can not Help

    pity  

     

    Kann keiner Helfen

     

    Schade

    DH
    Please use plain text.
    *Expert Elite*
    Posts: 1,647
    Registered: ‎04-29-2006

    Re: Attribute to move to block without the old ATT

    09-12-2012 12:00 AM in reply to: heinz.dober

    Hi,

     

    After adding the attribute definition to the block definition, you have to add the new attribute refrence to all block refrences in the database (i.e. synchronize only the new attribute).

    Gilles Chanteau
    Please use plain text.
    Active Contributor
    Posts: 37
    Registered: ‎02-14-2008

    Re: Attribute to move to block without the old ATT

    09-12-2012 12:18 AM in reply to: heinz.dober

    Thanks for reply
    Unfortunately, I have no idea about. Net
    I hoped that such a defense program has been.
    Thank you

     

    Danke für Antwort
    Leider habe ich keine Ahnung von .net
    ich habe gehofft das wehr so ein Programm schon hat.
    Danke

    DH
    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,371
    Registered: ‎10-08-2008

    Re: Attribute to move to block without the old ATT

    09-12-2012 06:49 AM in reply to: heinz.dober

    Try this code, just you have to change block name,

    new tag, prompt and default attribute value:

     

            //using Autodesk.AutoCAD.Interop.Common;
            //using Autodesk.AutoCAD.Interop;
            // Add new attribute and synchronize all blockreferences
    
    
    
            [CommandMethod("nea")]
            public static void AddNewAtt()
            {
    
                Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
                Database db = doc.Database;
                Editor ed = doc.Editor;
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead, false);
    
                    if (!bt.Has("PART"))
                    {
                        ed.WriteMessage("\nBlock definition TEST does not  exist");
                        return;
                    }
    
                    List<AttributeDefinition> attColl = new List<AttributeDefinition>();
    
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt["PART"], OpenMode.ForRead, false);
                    
                    foreach (ObjectId adefId in btr)
                    {
                        DBObject attObj = (DBObject)tr.GetObject(adefId, OpenMode.ForRead, false);
                        
                        if (attObj.GetRXClass().Name == "AcDbAttributeDefinition")
                        {
                            AttributeDefinition attObjDef = attObj as AttributeDefinition;
                            if (attObjDef != null)
                            {
    
                                attColl.Add(attObjDef);
                            }
                        }
                    }
                    AttributeDefinition[] atts = new AttributeDefinition[] { };
    
                    atts = attColl.ToArray();
                    //sort attribute location by X then by Y
                    Array.Sort(atts, (AttributeDefinition x, AttributeDefinition y) => 
                        ((x.Position.Y.CompareTo(y.Position.Y)) +
                        (y.Position.X.CompareTo(x.Position.X))));
    
    
                    // get a location of the last AttributeDefinition in the 
                    // block definition
    
                    AttributeDefinition lastAtt = atts[0];
                  
                  Point3d Oldloc = lastAtt.Position;
                  double attTxtHeight = lastAtt.Height;
                  ed.WriteMessage("\n{0:f3}", attTxtHeight);
                    // calculate location of the new AttributeDefinition in the 
                    // block definition 
                    
                  Point3d ptNewloc = new Point3d(Oldloc.X, Oldloc.Y - attTxtHeight * 1.97, 0) + btr.Origin.GetAsVector();
                    //// create a AttributeDefinition
                  // specify the text,tag and prompt
    
                  string strValue = "[ Enter a new value here ]";
    
                  string strTag = "[ Enter a new tag here ]";
    
                  string strPrompt = "[ Enter a new prompt here ]";
                  // used text style of the last attribute definition
                  AttributeDefinition attDef = new AttributeDefinition(ptNewloc, strValue, strTag, strPrompt, lastAtt.TextStyleId);
                  attDef.Height = 0.12;
                  attDef.Layer = "0";
                  attDef.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci, 0);
                  attDef.LinetypeId = lastAtt.LinetypeId;
                  attDef.Height = lastAtt.Height;
                  attDef.TextStyleId = lastAtt.TextStyleId;
    
                  attDef.Annotative = lastAtt.Annotative;
                  attDef.Preset = lastAtt.Preset;
                  attDef.Invisible = lastAtt.Invisible;
                  attDef.Constant = lastAtt.Constant;
                  attDef.Justify = lastAtt.Justify;
                  attDef.AlignmentPoint = ptNewloc;
                  attDef.LockPositionInBlock = lastAtt.LockPositionInBlock;
                  attDef.AdjustAlignment(db);
                  // append the AttributeDefinition to the definition 
                  btr.UpgradeOpen();
                  btr.AppendEntity(attDef);
                  tr.AddNewlyCreatedDBObject(attDef, true);
                  btr.DowngradeOpen();
    
                    tr.Commit();
    
                    Autodesk.AutoCAD.Interop.AcadDocument acdoc = (Autodesk.AutoCAD.Interop.AcadDocument)doc.AcadDocument;
    
                    AcadBlock acBlk = btr.AcadObject as AcadBlock;
    
                    acdoc.SendCommand("_.ATTSYNC _N " + acBlk.Name + "\n");
    
                }
            }

     Tested on A2010 only

     

    ~'J'~

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Active Contributor
    Posts: 37
    Registered: ‎02-14-2008

    Re: Attribute to move to block without the old ATT

    09-12-2012 11:06 AM in reply to: heinz.dober

    Thanks for the help only times

     

    Unfortunately it can not start
    Get error message

     

    Kann es leider nicht starten

    Bekommen Fehlermeldung

     

    Fehler 1 Klasse, Delegat, Enumeration, Schnittstelle oder Struktur erwartet.

    Fehler 3 Bezeichner erwartet

    DH
    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,371
    Registered: ‎10-08-2008

    Re: Attribute to move to block without the old ATT

    09-12-2012 11:21 AM in reply to: heinz.dober

    Try add namespace

    using System.Linq;

    Or use your own method to calculate position

    of last attribute wthin the block definition

     

    ~'J'~

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Active Contributor
    Posts: 37
    Registered: ‎02-14-2008

    Re: Attribute to move to block without the old ATT

    09-12-2012 11:12 PM in reply to: heinz.dober

    Will not run for
    these are my settings
    Thank you

     

    Bekomme das nicht zum laufen

    das sind meine einstellungen

    Danke

     

     

    using System;
    using Autodesk.AutoCAD.Runtime;
    using Autodesk.AutoCAD.ApplicationServices;
    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.Geometry;
    using Autodesk.AutoCAD.EditorInput;
    using Autodesk.AutoCAD.Interop.Common;
    using Autodesk.AutoCAD.Interop;
    using System.Linq;

    Add new attribute and synchronize all blockreferences

    DH
    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,371
    Registered: ‎10-08-2008

    Re: Attribute to move to block without the old ATT

    09-13-2012 05:40 AM in reply to: heinz.dober

    Just a question

    What is your Acad version, because I'm using a A2010 only?

     

    ~'J'~

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Active Contributor
    Posts: 37
    Registered: ‎02-14-2008

    Re: Attribute to move to block without the old ATT

    09-13-2012 05:42 AM in reply to: heinz.dober

    Hello

    Autocad 2013

     

    Hallo 

     

    Autocad 2013

    DH
    Please use plain text.