.NET

.NET

Reply
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 11 of 36 (350 Views)

Re: Attribute to move to block without the old ATT

09-13-2012 08:08 AM 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
Active Contributor
cadheinz
Posts: 37
Registered: ‎02-14-2008
Message 12 of 36 (349 Views)

Re: Attribute to move to block without the old ATT

09-13-2012 08:52 AM 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
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 13 of 36 (347 Views)

Re: Attribute to move to block without the old ATT

09-13-2012 09:07 AM 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
Active Contributor
cadheinz
Posts: 37
Registered: ‎02-14-2008
Message 14 of 36 (342 Views)

Re: Attribute to move to block without the old ATT

09-13-2012 09:52 AM 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
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 15 of 36 (336 Views)

Re: Attribute to move to block without the old ATT

09-13-2012 10:21 AM in reply to: cadheinz

Try to remove static from void

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 16 of 36 (330 Views)

Re: Attribute to move to block without the old ATT

09-13-2012 10:53 AM 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
Active Contributor
cadheinz
Posts: 37
Registered: ‎02-14-2008
Message 17 of 36 (328 Views)

Re: Attribute to move to block without the old ATT

09-13-2012 11:11 AM in reply to: Hallex

Thank you'll morning testing have not got Autocad

 

Danke werde das Morgen Testen habe hier kein Autocad

 

 

DH
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 18 of 36 (321 Views)

Re: Attribute to move to block without the old ATT

09-13-2012 12:13 PM 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
Active Contributor
cadheinz
Posts: 37
Registered: ‎02-14-2008
Message 19 of 36 (306 Views)

Re: Attribute to move to block without the old ATT

09-13-2012 09:45 PM 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
Active Contributor
cadheinz
Posts: 37
Registered: ‎02-14-2008
Message 20 of 36 (303 Views)

Re: Attribute to move to block without the old ATT

09-13-2012 10:03 PM 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
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.