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
Solved! Go to Solution.
Solved by Gepaha. Go to Solution.
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:
Just a side note: you don't move objects to a scale, you apply a scale to an object 🙂
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
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();
}
}
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
Can't find what you're looking for? Ask the community or share your knowledge.