.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Attribute to move to block without the old ATT

35 REPLIES 35
Reply
Message 1 of 36
cadheinz
1057 Views, 35 Replies

Attribute to move to block without the old ATT

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
35 REPLIES 35
Message 2 of 36
cadheinz
in reply to: cadheinz

Can not Help

pity  

 

Kann keiner Helfen

 

Schade

DH
Message 3 of 36
_gile
in reply to: cadheinz

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
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 36
cadheinz
in reply to: cadheinz

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
Message 5 of 36
Hallex
in reply to: cadheinz

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
Message 6 of 36
cadheinz
in reply to: cadheinz

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
Message 7 of 36
Hallex
in reply to: cadheinz

Try add namespace

using System.Linq;

Or use your own method to calculate position

of last attribute wthin the block definition

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 8 of 36
cadheinz
in reply to: cadheinz

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
Message 9 of 36
Hallex
in reply to: cadheinz

Just a question

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

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 10 of 36
cadheinz
in reply to: cadheinz

Hello

Autocad 2013

 

Hallo 

 

Autocad 2013

DH
Message 11 of 36
Hallex
in reply to: cadheinz

Well,

I haven't have this version on my machine,

but I will try to rewrite the code without using Interop later,

no warrantee though,

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 12 of 36
cadheinz
in reply to: Hallex

Thank you

If that would not be possible, I prefer to Lisp, a little?

 

Ich danke Ihnen

 

Wäre das in Lisp nicht möglich?

DH
Message 13 of 36
Hallex
in reply to: cadheinz

Try this code instead, but you have to invoke command ATTSYNC

manually after:

 

        //        #region "System"

        //using System;
        //using System.IO;
        //using System.Collections.Generic;
        //using System.Text;
        //using System.Reflection;

        //      #endregion


        //        #region "AutoCAD Imports"

        //using Autodesk.AutoCAD.Runtime;
        //using Autodesk.AutoCAD.ApplicationServices;
        //using Autodesk.AutoCAD.DatabaseServices;
        //using Autodesk.AutoCAD.Geometry;
        //using Autodesk.AutoCAD.EditorInput;
        //using Autodesk.AutoCAD.Colors;

        //      #endregion

        // import accoremgd.dll
        [CommandMethod("nea")]
        public static void AddNewAtt()
        {
            string blkName = "PART";
            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(blkName))
                {
                    ed.WriteMessage("\nBlock definition TEST does not  exist");
                    return;
                }

                double dx = new double();
                double dy = new double();
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[blkName], OpenMode.ForRead, false);
                
                int cnt = 0;
                AttributeDefinition matchAtt = new AttributeDefinition();
                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 (cnt == 0)
                        {
                            matchAtt = attObjDef;
                            dx = matchAtt.Position.X;
                            dy = matchAtt.Position.Y;
                            cnt += 1;
                        }
                        if (attObjDef != null)
                        {
                            Point3d ip = attObjDef.Position;
                            if ((attObjDef.Position.X >= dx) && (attObjDef.Position.Y <= dy))
                            {
                                dx = attObjDef.Position.X;
                                dy = attObjDef.Position.Y;
                            }
                        }
                    }
                }


                // get a location of the last AttributeDefinition in the 
                // block definition
                Point3d Oldloc = matchAtt.Position;
                double attTxtHeight = matchAtt.Height;
                ed.WriteMessage("\n{0:f3}", attTxtHeight);
                // calculate location of the new AttributeDefinition in the 
                // block definition 

                Point3d ptNewloc = new Point3d(dx, dy - attTxtHeight * 1.97, btr.Origin.Z) + 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, matchAtt.TextStyleId);
                attDef.Height = matchAtt.Height;
                attDef.Layer = "0";
                attDef.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci, 0);
                attDef.LinetypeId = matchAtt.LinetypeId;
                attDef.Height = matchAtt.Height;
                attDef.TextStyleId = matchAtt.TextStyleId;

                attDef.Annotative = matchAtt.Annotative;
                attDef.Preset = matchAtt.Preset;
                attDef.Invisible = matchAtt.Invisible;
                attDef.Constant = matchAtt.Constant;
                attDef.Justify = matchAtt.Justify;
                attDef.AlignmentPoint = ptNewloc;
                attDef.LockPositionInBlock = matchAtt.LockPositionInBlock;
                attDef.AdjustAlignment(db);
                // append the AttributeDefinition to the definition 
                btr.UpgradeOpen();
                btr.AppendEntity(attDef);
                tr.AddNewlyCreatedDBObject(attDef, true);
                btr.DowngradeOpen();

                tr.Commit();

                ed.WriteMessage("nPerform \"_ATTSYNC _N " + blkName + "\" + command manually");
              
            }
        }

 Change block name within the code

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 14 of 36
cadheinz
in reply to: Hallex

Thanks for the effort.

But unfortunately, I still have an error message

 

 

Danke für die Mühe.

 

Aber leider habe ich immer noch einen Fehlermeldung 

DH
Message 15 of 36
Hallex
in reply to: cadheinz

Try to remove static from void

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 16 of 36
Hallex
in reply to: cadheinz

Try this lisp you asked for

 

(defun add_att_to_block	(data_list /   acsp adoc att attpt atts blk blkent blk_obj blocks bname hgt inspt lastatt orig pt pts rot)
  (vl-load-com)
  (setq	adoc (vla-get-activedocument
	       (vlax-get-acad-object)
	     )
  )
  (if (= 1 (vlax-get-property adoc 'Activespace))
    (setq acsp (vla-get-modelspace adoc))
    (setq acsp (vla-get-paperspace adoc))
  )
  (setq blocks (vla-get-blocks adoc))
  (vla-startundomark adoc)
  
  (setq	blk	(vlax-ename->vla-object (setq blkent(car (entsel "\nSelect attributed block: "))))
	inspt (vlax-get blk 'insertionpoint)
	bname	(vla-get-effectivename blk)
	blk_obj	(vla-item blocks bname)
	orig (vlax-get blk_obj 'origin)
	atts (vlax-invoke blk 'Getattributes)
  )
  (setq pts nil)
  (foreach att atts
    (setq pts (cons (vlax-get att 'insertionpoint) pts)))
  (setq pts (vl-sort pts '(lambda( a b) (and (>= (car a)(car b))(<= (cadr a)(cadr b))))))
  (setq pt (car pts))
  
  (setq lastatt (last (vlax-invoke blk 'Getattributes)))
  (setq hgt (vla-get-height lastatt))
  (setq rot (vla-get-rotation lastatt))  
  (setq attpt (mapcar '-  (list (car  pt)(- (cadr pt)(* hgt 1.97)) (caddr pt)) inspt ))
  (setq	att (vla-AddAttribute
	      blk_obj
	      hgt
	      acAttributeModePreset
	      (car data_list)
	    (vlax-3d-point attpt)
	      (cadr data_list)
	      (caddr data_list)
	    )
  )
  (vla-put-alignment att (vla-get-alignment lastatt))
  (vla-put-textalignmentpoint
    att
    (vlax-3d-point attpt)

  )
  (vla-put-rotation att rot)
  (vla-update (vlax-get-acad-object))
  (vlax-release-object blk)
  (vlax-release-object blk_obj)
  (setvar "cmdecho" 0)
  (command "_attsync" "_N" bname)
  (setvar "cmdecho" 1)
  (vla-regen adoc acactiveviewport)
  (vla-endundomark adoc)
  (princ)
)

;;usage:
(defun C:ia()
  ;; add Prompt, Tag and default value in list:
(add_att_to_block (list "Prompt" "TAG" "Value"))
  (princ)
  )

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 17 of 36
cadheinz
in reply to: Hallex

Thank you'll morning testing have not got Autocad

 

Danke werde das Morgen Testen habe hier kein Autocad

 

 

DH
Message 18 of 36
Hallex
in reply to: cadheinz

Try code from attached text file

don't forget to rename block name and

your project name within the class

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 19 of 36
cadheinz
in reply to: Hallex

Thank you
The Lisp works but not quite what I mean
The old attribute should not be moved

 

Danke
Das Lisp funktioniert aber nicht ganz so wie ich meine
Das alte Attribut sollte nicht verschoben werden

 

 

DH
Message 20 of 36
cadheinz
in reply to: Hallex

I said again. Net now runs
Super Thank you
But the old Att moves
and I should be able to select the block
Thank you very much
they are the best

 

So ich nochmal das .net läuft jetzt auch
Super Danke
aber das alte Att wird verschoben
und ich sollte den Block auswählen können
Vielen Dank
sie sind der Beste

DH

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost