Author Topic: Get annotation scales per object  (Read 2550 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
Get annotation scales per object
« on: March 25, 2011, 12:52:49 PM »
I was wondering if there was a better way of doing this then using the ' HasContext ' method on each entity per each scale.  I tried to step through the extension dictionary, but can't seem to figure out the final step with .Net.  I can get there with lisp, to see the scales, but not with .Net.  Here is the section of code I'm talking about.  The objects that are returned in the spot marked are ' ACDBBLOCKREFOBJECTCONTEXTDATACLASS ' and I can see no way to use them, and they reference the annotation scale, so I need to get some information from them.

I can't cast them to an AnnotationScale or ObjectContext, as they are not from the DBObject class.

Code: [Select]
foreach ( ObjectId id in btr ) {
Entity Ent = Trans.GetObject( id, OpenMode.ForRead ) as Entity;
if ( Ent.Annotative == AnnotativeStates.False ) continue;
List<AnnotationScale> tempScList = new List<AnnotationScale>();
//foreach ( AnnotationScale asc in ScList ) {
// if ( Ent.HasContext( asc ) ) tempScList.Add( asc );
//}
ObjectId ExId = Ent.ExtensionDictionary;
if ( ExId.IsNull ) continue;
DBDictionary ExDict = Trans.GetObject( ExId, OpenMode.ForRead ) as DBDictionary;
if ( !ExDict.Contains( "AcDbContextDataManager" ) ) continue;
DBDictionary CdmDict = Trans.GetObject( ExDict.GetAt( "AcDbContextDataManager" ), OpenMode.ForRead ) as DBDictionary;
if ( !CdmDict.Contains( "ACDB_ANNOTATIONSCALES" ) ) continue;
DBDictionary AnsDict = Trans.GetObject( CdmDict.GetAt( "ACDB_ANNOTATIONSCALES" ), OpenMode.ForRead ) as DBDictionary;
foreach ( DBDictionaryEntry de in AnsDict ) {
// this is the problem area
}
foreach ( AnnotationScale asc in tempScList ) ScList.Remove( asc );
}
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Get annotation scales per object
« Reply #1 on: March 25, 2011, 01:50:03 PM »
Here is some code I have messing with Scales

I thought Viewport were the only ones with AnnotationScale property?
 
Code: [Select]

        [CommandMethod("AddTWilleyScale")]
        static public void AddTWilleyScale()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            ObjectContextManager contextManager = db.ObjectContextManager;

            if (contextManager == null)
                return;

            ObjectContextCollection contextCollection = contextManager.GetContextCollection("ACDB_ANNOTATIONSCALES");

            if (contextCollection == null)
                return;
            AnnotationScale annoScale = new AnnotationScale();

            annoScale.Name = "TWileyScale";
            annoScale.PaperUnits = 1;
            annoScale.DrawingUnits = 23;

            contextCollection.AddContext(annoScale);                               
           
        }




        [CommandMethod("PrintScaleList")]
        static public void PrintScaleList()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            ObjectContextManager contextManager = db.ObjectContextManager;

            if (contextManager == null)
                return;

            ObjectContextCollection contextCollection = contextManager.GetContextCollection("ACDB_ANNOTATIONSCALES");


            if (contextCollection == null)
                return;


            foreach (ObjectContext objCxt in contextCollection)
            {
                AnnotationScale scale = objCxt as AnnotationScale;

                ed.WriteMessage("\n AnnoScale is  " + scale.Name);
            }


        }
           
       


        [CommandMethod("ChangeVpAnnoScale")]
        static public void ChangeVpAnnoScale()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            PromptEntityOptions peo = new PromptEntityOptions("\nSelect Viewport: ");
            peo.SetRejectMessage("\nInvalid selection...");
            peo.AddAllowedClass(typeof(Viewport), true);

            PromptEntityResult per = ed.GetEntity(peo);

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

            using (Transaction trx = db.TransactionManager.StartTransaction())
            {
                Viewport vp = per.ObjectId.GetObject(OpenMode.ForWrite) as Viewport;

                ObjectContextManager contextMgr = db.ObjectContextManager;

                ObjectContextCollection contextCollection = contextMgr.GetContextCollection("ACDB_ANNOTATIONSCALES");

                AnnotationScale scale = contextCollection.GetContext("TWileyScale") as AnnotationScale;

                vp.AnnotationScale = scale;

                trx.Commit();
            }
        }


T.Willey

  • Needs a day job
  • Posts: 5251
Re: Get annotation scales per object
« Reply #2 on: March 25, 2011, 02:44:43 PM »
Viewports are the only ones that have that property ( AnnotationScale ) that you can get that way.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

dan.glassman

  • Guest
Re: Get annotation scales per object
« Reply #3 on: March 25, 2011, 02:56:36 PM »
Code: [Select]
[color=blue]using Autodesk.AutoCAD.Internal;[/color]
Code: [Select]
[CommandMethod("GetEntityScales")]
public void GetEntScales()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Editor ed = doc.Editor;
    Database db = doc.Database;
    
    PromptEntityOptions peo = new PromptEntityOptions("Select an ent: ");
    PromptEntityResult per = ed.GetEntity(peo);
    if (per.Status != PromptStatus.OK) return;

    using (Transaction t = db.TransactionManager.StartTransaction())
    {
        Entity e = t.GetObject(per.ObjectId, OpenMode.ForRead) as Entity;
        if (e == null) return;

        [color=blue]List<ObjectContext> contexts =
            ObjectContexts.GetContexts(e, "ACDB_ANNOTATIONSCALES");[/color]
        foreach (ObjectContext context in contexts)
        {
            [color=blue]AnnotationScale scale = (AnnotationScale)context;[/color]
            ed.WriteMessage("{0} ({1}:{2})", scale.Name, scale.PaperUnits, scale.DrawingUnits);
        }
    }
}

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Get annotation scales per object
« Reply #4 on: March 25, 2011, 03:01:29 PM »
Is that new Dan?  I don't seem to have it in 2009.   :cry:
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

dan.glassman

  • Guest
Re: Get annotation scales per object
« Reply #5 on: March 25, 2011, 03:06:11 PM »
whoops -- edited for dll name.

Hrm; I never checked in the days of yore and am not sure I have a copy lying around.  Autodesk.AutoCAD.Internal in '09 requires an additional reference, though -- add a reference to acmgdinternal.dll in your Program Files folder [unless you already did...].

dan.glassman

  • Guest
Re: Get annotation scales per object
« Reply #6 on: March 25, 2011, 03:10:36 PM »
Hrm; I never checked in the days of yore and am not sure I have a copy lying around.  Autodesk.AutoCAD.Internal in '09 requires an additional reference, though -- add a reference to acmgdinternal.dll in your Program Files folder [unless you already did...].

I do still have it.  Once you add that reference and the Using Autodesk.AutoCAD.Internal, you should be golden.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Get annotation scales per object
« Reply #7 on: March 25, 2011, 03:33:04 PM »
Thanks Dan.  That worked.

Now I got other trouble though.   :-D  If I can't figure that out I'll be back.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Get annotation scales per object
« Reply #8 on: March 25, 2011, 03:47:27 PM »
All is working now as it should.  Thanks again.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

dan.glassman

  • Guest
Re: Get annotation scales per object
« Reply #9 on: March 25, 2011, 03:54:19 PM »
You're welcome.