Alternative for ObjectID for formatting

Alternative for ObjectID for formatting

stefanveurink68AXD
Advocate Advocate
555 Views
4 Replies
Message 1 of 5

Alternative for ObjectID for formatting

stefanveurink68AXD
Advocate
Advocate

I've got a menu in which you can select an object with an standard format (textformat, height, style etc. for example), and after that there are several functions who use that selected object to format the for example new texts. 

 

The object is referred to by the objectId, if there was never a text selected, its set at objectId.Null. 

 

This works fine when the object is in the active drawing, however, when working in another drawing (wether the original drawing is opened or closed) this off course doesn't work anymore. 

 

I'm not really wanting to set all the properties by hand as different values, for for example dimensions this is way to much work. 

 

So... Anyone got a smart idea on how to store such an object in your menu with which you can allways access it? Or do I really have to refer to the drawing as an side-database (and is this working when the drawing with the object IS the active document?)

 

Any opinions on this would be very welcome cause I don't know what's smart to do at this moment. 


Thanks. 

0 Likes
556 Views
4 Replies
Replies (4)
Message 2 of 5

SENL1362
Advisor
Advisor

The Object Handle is Persistent between Sessions, see ObjectId.Handle

var handle=id.Handle;

 

//Stored as String, convert back to Handle and then to ObjectId

var handleAsInt=Convert.ToInt64(handle.ToString(), 16);

var newHandle = new Handle(handleAsInt);

var id = db.GetObjectId(false, newHandle, 0);

 

 

Message 3 of 5

fieldguy
Advisor
Advisor

>>I've got a menu in which you can select an object<<

maybe you could create an instance of that object in your code to reside in memory. you have to use "Dispose()" method on that object at some point - if it is not added to the database.     

Message 4 of 5

stefanveurink68AXD
Advocate
Advocate

Can't get it working yet. the solution from @fieldguy only seems to work as long is the original drawing is opened (although not active). If not opened, the code I got from https://spiderinnet1.typepad.com/blog/2012/03/autocad-net-copy-properties-between-different-kind-ent... isn't working. By the way this code is very slow but that's an other issue. 

 

I first tried the handle method from @SENL1362 but then is my question, when calling the object by its id / handle, how do I know which database to choose in transaction.getobject(id, openmode.forRead). And by the way don't know if the matchproperties method is working then, suspect it to give the same problem. 

 

Maybe next week brings me more luck 🙂

0 Likes
Message 5 of 5

stefanveurink68AXD
Advocate
Advocate

Okay you guys probably think I'm crazy (and might be right with that😁) but purely out of frustration I built this class, which results in the thing I described: 

 

public class FormatText
    {
        public bool ingevuld; 

        double TextHeight;                              
        string layer;
        TextHorizontalMode horizontalmode;
        bool ismirrorinx;
        bool ismirrorediny;
        AttachmentPoint justify;
        double widhtfactor;
        Autodesk.AutoCAD.Colors.Color color;             
        string linetype;                                
        double linetypescale;                           
        Autodesk.AutoCAD.Colors.Transparency transparency;
        double thickness;
        string textstylename;
        double oblique;
        Vector3d MDirection;
        double MLineSpaceDistance;
        double MLineSpacingFactor;
        LineSpacingStyle Mlinespacingstyle;
        bool MBackgroundFill;
        Autodesk.AutoCAD.Colors.Color MBackgroundFillColor;
        double BackgroundScaleFactor;
        Autodesk.AutoCAD.Colors.Transparency MBackgroundTransparency;
        bool UseBackgroundColor;
        AttachmentPoint MAttachment;
        //??Textframe?//





        public FormatText()
        {
            ingevuld = false;
        }


        public void GetProperties(MText toGet)
        {
            ingevuld = true;

            try { TextHeight = toGet.TextHeight; } catch { }
            try { layer = toGet.Layer; } catch { }
            try { justify = toGet.Attachment; } catch { }
            try { color = toGet.Color; } catch { }
            try { linetype = toGet.Linetype; } catch { }
            try { linetypescale = toGet.LinetypeScale; } catch { }
            try { transparency = toGet.Transparency; } catch { }
            try { textstylename = toGet.TextStyleName; } catch { }
            try { MDirection = toGet.Direction; } catch { }
            try { MLineSpaceDistance = toGet.LineSpaceDistance; } catch { }
            try { MLineSpacingFactor = toGet.LineSpacingFactor; } catch { }
            try { Mlinespacingstyle = toGet.LineSpacingStyle; } catch { }
            try { UseBackgroundColor = toGet.UseBackgroundColor; } catch { }
            if (UseBackgroundColor == true)
            {
                try { MBackgroundFill = toGet.BackgroundFill; } catch { }
                try { MBackgroundFillColor = toGet.BackgroundFillColor; } catch { }
                try { BackgroundScaleFactor = toGet.BackgroundScaleFactor; } catch { }
                try { MBackgroundTransparency = toGet.BackgroundTransparency; } catch { }
            }



        }

        public void GetProperties(DBText toGet)
        {
            ingevuld = true;

            try { TextHeight = toGet.Height; } catch { }
            try { layer = toGet.Layer; } catch { }
            try { horizontalmode = toGet.HorizontalMode; } catch { }
            try { ismirrorinx = toGet.IsMirroredInX; } catch { }
            try { ismirrorediny = toGet.IsMirroredInY; } catch { }
            try { justify = toGet.Justify; } catch { }
            try { widhtfactor = toGet.WidthFactor; } catch { }
            try { color = toGet.Color; } catch { }
            try { linetype = toGet.Linetype; } catch { }
            try { linetypescale = toGet.LinetypeScale; } catch { }
            try { transparency = toGet.Transparency; } catch { }
            try { thickness = toGet.Thickness; } catch { }
            try { textstylename = toGet.TextStyleName; } catch { }
            try { oblique = toGet.Oblique; } catch { }
            
            try { UseBackgroundColor = false; } catch { }






        }


        public MText SetMtextProperties(MText toSet)
        {
            try { toSet.TextHeight = TextHeight; } catch { }
            try { toSet.Layer = layer; } catch { }
            try { toSet.Attachment = justify; } catch { }
            try { toSet.Color = color; } catch { }
            try { toSet.Linetype = linetype; } catch { }
            try { toSet.LinetypeScale = linetypescale; } catch { }
            try { toSet.Transparency = transparency; } catch { }

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            try
            {
                using (DocumentLock docLock = doc.LockDocument())
                {
                    using (Transaction tr = doc.TransactionManager.StartTransaction())
                    {
                        TextStyleTable txt = (TextStyleTable)tr.GetObject(db.TextStyleTableId, OpenMode.ForRead);
                        toSet.TextStyleId = txt[textstylename];
                        tr.Commit(); 
                    }
                }
            }
            catch { }


            try { toSet.Direction = MDirection; } catch { }
            try { toSet.LineSpaceDistance = MLineSpaceDistance; } catch { }
            try { toSet.LineSpacingFactor = MLineSpacingFactor; } catch { }
            try { toSet.LineSpacingStyle = Mlinespacingstyle; } catch { }
            try { toSet.UseBackgroundColor = UseBackgroundColor; } catch { }
            if(UseBackgroundColor == true) { 
            try { toSet.BackgroundFill = MBackgroundFill; } catch { }
            try { toSet.BackgroundFillColor = MBackgroundFillColor; } catch { }
            try { toSet.BackgroundScaleFactor = BackgroundScaleFactor; } catch { }
            try { toSet.BackgroundTransparency = MBackgroundTransparency; } catch { }
        }

            return toSet;
        }

        public DBText SetDBtextProperties(DBText toSet)
        {
            try { toSet.Height = TextHeight; } catch { }
            try { toSet.Layer = layer; } catch { }
            try { toSet.HorizontalMode = horizontalmode; } catch { }
            try { toSet.IsMirroredInX = ismirrorinx; } catch { }
            try { toSet.IsMirroredInY = ismirrorediny; } catch { }
            try { toSet.Justify = justify; } catch { }
            try { toSet.WidthFactor = widhtfactor; } catch { }
            try { toSet.Color = color; } catch { }
                try { toSet.Linetype = linetype; } catch { }
                try { toSet.LinetypeScale = linetypescale; } catch { }
                try { toSet.Transparency = transparency; } catch { }
                try { toSet.Thickness = thickness; } catch { }

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            try
            {
                using (DocumentLock docLock = doc.LockDocument())
                {
                    using (Transaction tr = doc.TransactionManager.StartTransaction())
                    {
                        TextStyleTable txt = (TextStyleTable)tr.GetObject(db.TextStyleTableId, OpenMode.ForRead);
                        toSet.TextStyleId = txt[textstylename];
                        tr.Commit();
                    }
                }
            }
            catch { }


            try { toSet.Oblique = oblique; } catch { }
               
                return toSet;
        }
    }

 

Anyway it works. And it works a lot quicker then the matchpropertiesmethod i mentioned earlier by the way. 

 

Please enlighten me. Is this the best way to do it?

Or, is there I much quicker way?

I hope so cause I want to do the same thing for dimensions, multileaders, and maybe more. 

 

If not, I expect someone else to find the code usefull as well that's why I posted the whole thing

0 Likes