Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Moving Annotative Object to Current Anno Scale

My_Civil_3D
Advocate

Moving Annotative Object to Current Anno Scale

My_Civil_3D
Advocate
Advocate

I am trying to make a command that the user selects items in the drawing and the goal is to remove the current annotative scales for that item and  move it to the current set annotative scale. i dont just want to add the current it needs to remove the old ones first.

the reason why i split the different objects is because i assume that they all have different ways to set the annotative scales. the alert dialog box was to check if i selects a correct object. This Is what i have so far but i am stuck

  public void AttachToCurrentAnnoScale()
  {
      Document doc = Application.DocumentManager.MdiActiveDocument;
      Database db = doc.Database;
      Editor edt = doc.Editor;

      ObjectContextManager ocm = db.ObjectContextManager;
      ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");
      AnnotationScale currentAnnoScale = db.Cannoscale;

      Application.MainWindow.Focus();

      using (DocumentLock docLock = doc.LockDocument())
      {
          using (Transaction trans = db.TransactionManager.StartTransaction())
          {
              //Create TypedValue
              TypedValue[] types = new TypedValue[]
                  {
          new TypedValue((int)DxfCode.Operator, "<or"),
          new TypedValue((int)DxfCode.Start, "INSERT"),
          new TypedValue((int)DxfCode.Start, "MTEXT"),
          new TypedValue((int)DxfCode.Start, "MULTILEADER"),
          new TypedValue((int)DxfCode.Start, "DIMENSION"),
          new TypedValue((int)DxfCode.Operator, "or>")
                  };

              //Create SelectionFilter
              SelectionFilter filter = new SelectionFilter(types);

              //Create PromptStringResult

              PromptSelectionResult selResult = edt.GetSelection(filter);

              //Selection Verification
              if (selResult.Status == PromptStatus.OK)
              {
                  //Create Selection Set
                  SelectionSet ss = selResult.Value;
                  //Loop Thru Selection Set
                  foreach (SelectedObject selObj in ss)
                  {
                      DBObject dbobj = trans.GetObject(selObj.ObjectId, OpenMode.ForWrite);
                      if (dbobj != null)
                      {
                          //Check For Block
                          if (dbobj is BlockReference blockref)
                          {
                              Application.ShowAlertDialog("You Selected a Block");

                          }

                          //Check For Mtext
                          if (dbobj is MText mtext)
                          {
                              mtext.Annotative = AnnotativeStates.True;
                              Application.ShowAlertDialog("You Selected a MTEXT");
                              
                          }

                          //Check for MLeader
                          if (dbobj is MLeader mleader)
                          {
                              mleader.Annotative = AnnotativeStates.True;
                              Application.ShowAlertDialog("You Selected a MLeader");
                          }

                          //Check for Dimension
                          if (dbobj is Dimension dim)
                          {
                              dim.Annotative = AnnotativeStates.True;
                              Application.ShowAlertDialog("You Selected a Dimension");
                          }


                      }
                  }
                  trans.Commit();
                  edt.WriteMessage("\nObject Moved to Current Annotation Scale.");
              }
              else
              {
                  edt.WriteMessage("\nNo valid entities selected. Ensure the correct types are being selected.");
              }
          }
      }
  }

 THis 

Civil 3D Certified Professional
0 Likes
Reply
Accepted solutions (1)
404 Views
4 Replies
Replies (4)

Anton_Huizinga
Advocate
Advocate

You can use dbobj.RemoveContext() in a loop for all Annotation Scales, then dbobj.AddContext() to add the current one. Here you find some examples btw:

 

https://www.keanw.com/2011/10/delete-all-but-current-annotation-scales-on-autocad-objects-using-net....

 

Just a side note: you don't move objects to a scale, you apply a scale to an object 🙂

0 Likes

My_Civil_3D
Advocate
Advocate

this works but there is an issue when it comes to mleaders. it applies the scales but it doesnt change. i have a lisp file i have used for a long time and when using the lisp it works fine but this doesnt seem to work correctly with mleaders even though they are set annotative and changes when changing manually

Civil 3D Certified Professional
0 Likes

Gepaha
Collaborator
Collaborator
Accepted solution

 

        public void AddCurrentAnnoScaleAndRemoveOthers()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            TypedValue[] types = new TypedValue[]
            {
                new TypedValue((int)DxfCode.Operator, "<or"),
                new TypedValue((int)DxfCode.Start, "INSERT"),
                new TypedValue((int)DxfCode.Start, "MTEXT"),
                new TypedValue((int)DxfCode.Start, "MULTILEADER"),
                new TypedValue((int)DxfCode.Start, "DIMENSION"),
                new TypedValue((int)DxfCode.Operator, "or>")
            };

            SelectionFilter sf = new SelectionFilter(types);
            PromptSelectionResult psr = ed.GetSelection(sf);

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

            ObjectId[] ids = psr.Value.GetObjectIds();

            ObjectContextManager ocm = db.ObjectContextManager;
            ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");

            if (ocm == null)
                return;          

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                foreach (ObjectId id in ids)
                {
                    Entity ent = trans.GetObject(id, OpenMode.ForWrite) as Entity;

                    if (ent == null)
                        continue;

                    if (ent.Annotative == AnnotativeStates.True)
                    {
                        if (!ent.HasContext(db.Cannoscale))                        
                            ent.AddContext(db.Cannoscale);

                        foreach (ObjectContext oc in occ)
                           if (ent.HasContext(oc) && oc.Name != db.Cannoscale.Name)
                                ent.RemoveContext(oc);                                       
                    }
                    else
                    {
                        ent.Annotative = AnnotativeStates.True;
                        ent.AddContext(db.Cannoscale);                       
                    }
                }
                trans.Commit();
            }
        }

 

0 Likes

My_Civil_3D
Advocate
Advocate

Very interesting. Thank you i am trying to learn as much as possible as i go along i have done some online course but it seems the mountain

Civil 3D Certified Professional
0 Likes