Hi there
I've added some RibbonButton that insert multiple entities in the drawing database, all these entities are then grouped together.
When CTRL-Z is clicked after the previous action, everything gets removed entity by entity.
How can I just remove the group on a ctrl-z and not all entities one by one?
Thank you
Alain
Solved! Go to Solution.
Solved by ActivistInvestor. Go to Solution.
Your question is very limited in that you do not describe how you are inserting the entities. Is it through code? If so then you need to show that code. We cannot guess about how you are inserting the objects and what you may or may not be doing right.
Code example:
Let's say this one here, a DrawBox() function.
Polyline plBox = new Polyline(4)
{
Closed = true,
Layer = layer,
Normal = Vector3d.ZAxis,
LineWeight = LineWeight.ByLayer
};
plBox.AddVertexAt(0, new Point2d(startPoint.X, startPoint.Y), 0.0, 1.0, 1.0);
Point3d endPoint = AcadTools.CalculateEndPoint(startPoint, length, orientationDepart);
plBox.AddVertexAt(1, new Point2d(endPoint.X, endPoint.Y), 0.0, 1.0, 1.0);
endPoint = AcadTools.CalculateEndPoint(endPoint, width, MathTools.AddAngle(orientationDepart, 90));
plBox.AddVertexAt(2, new Point2d(endPoint.X, endPoint.Y), 0.0, 1.0, 1.0);
endPoint = AcadTools.CalculateEndPoint(endPoint, length, MathTools.AddAngle(orientationDepart, 180));
plBox.AddVertexAt(3, new Point2d(endPoint.X, endPoint.Y), 0.0, 1.0, 1.0);
btr.AppendEntity(plBox);
Tx.AddNewlyCreatedDBObject(plBox, true);
Group.AppendEntity(plBox.ObjectId);
If you call the DrawBox() routine multiple times passing it the same Group..
DrawBox(Group1, StartPoint1, ...);
DrawBox(Group1, StartPoint2, ...);
DrawBox(Group1, StartPoint3, ...);
And then CTRL-Z (_u), each of the polylines added will be removed one by one, I would like to remove all of them at once.
Would be easy if the entities where be added to the group and then the group added to the database.
Am I doing something wrong?
Thank you
It looks like you only posted part of the function that you call Drawbox(), because I do not see any reference to a transaction starting or ending.
Unfortunately the parts that you omitted are relevant. When posting code in a message please use the insert code button on the toolbar.
internal static Polyline DrawBox(Database db, Group autocadGroup, string layer, Point3d startPoint, double length, double width, double orientationDepart, bool drawHatch, byte transparency = 0)
{
Polyline plBox = default;
AcadTools.AddLayerFromDatabase(layer);
using (DocumentLock docLock = NewGeboCAD.AcDoc.LockDocument())
{
using (Transaction Tx = db.TransactionManager.StartTransaction())
{
ObjectId ModelSpaceId = SymbolUtilityServices.GetBlockModelSpaceId(db);
BlockTableRecord btr = Tx.GetObject(ModelSpaceId, OpenMode.ForWrite) as BlockTableRecord;
plBox = new Polyline(4)
{
Closed = true,
Layer = layer,
Normal = Vector3d.ZAxis,
LineWeight = LineWeight.ByLayer
};
plBox.AddVertexAt(0, new Point2d(startPoint.X, startPoint.Y), 0.0, 1.0, 1.0);
Point3d endPoint = AcadTools.CalculateEndPoint(startPoint, length, orientationDepart);
plBox.AddVertexAt(1, new Point2d(endPoint.X, endPoint.Y), 0.0, 1.0, 1.0);
endPoint = AcadTools.CalculateEndPoint(endPoint, width, MathTools.AddAngle(orientationDepart, 90));
plBox.AddVertexAt(2, new Point2d(endPoint.X, endPoint.Y), 0.0, 1.0, 1.0);
endPoint = AcadTools.CalculateEndPoint(endPoint, length, MathTools.AddAngle(orientationDepart, 180));
plBox.AddVertexAt(3, new Point2d(endPoint.X, endPoint.Y), 0.0, 1.0, 1.0);
ObjectId pLineId = btr.AppendEntity(plBox);
Tx.AddNewlyCreatedDBObject(plBox, true);
ObjectIdCollection ObjIds = new ObjectIdCollection { pLineId };
Hatch oHatch = default;
if (drawHatch)
{
oHatch = new Hatch();
Vector3d normal = new Vector3d(0.0, 0.0, 1.0);
oHatch.Transparency = new Autodesk.AutoCAD.Colors.Transparency((byte)transparency);
oHatch.Normal = normal;
oHatch.Elevation = 0.0;
oHatch.PatternScale = 1.0;
oHatch.Layer = layer;
oHatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");
btr.AppendEntity(oHatch);
Tx.AddNewlyCreatedDBObject(oHatch, true);
oHatch.Associative = true;
oHatch.AppendLoop((int)HatchLoopTypes.Default, ObjIds);
oHatch.EvaluateHatch(true);
}
autocadGroup.AppendEntity(plBox.ObjectId);
if (drawHatch) autocadGroup.AppendEntity(oHatch.ObjectId);
Tx.Commit();
if (drawHatch) AcadTools.SetDrawOrder(oHatch, eDrawOrder.Bottom);
}
}
return plBox;
}
Sory about that, here is the full code to DrawBox() function
There is a separate undo of each object created because each time you create one object you start and end a transaction, which creates the equivalent of an undo group.
To solve that problem you need to decouple the code that draws the objects from the code that starts and commits the transaction and locks/unlocks the document.
That makes sense, so, if I did understand correctly, I should first lock the document, open a new transaction, happen entities in the database / transaction and then commit the transaction when completed?
Thank you!
Alain
Yes, there should only be one transaction and one document lock And while they are active you can draw any number of objects, and all of them will be undone as a group
Wow ok great,
Lot's of changes in my code, I will try this now and let you know of the results.
Thank you my friend 🙂
Alain
The changes aren't really that extensive. You just need to remove the code that starts and commits the transaction and locks and unlocks the document from the method that draws the objects. Then, pass a transaction into that method as an argument , that it can use to draw the objects.
The approach I'll be taking is more like:
DrawingHelper.StartTransaction(); // Lock and start a new transaction
DrawingHelper.DrawLine(...);
DrawingHelper.DrawBox(...);
DrawingHelper.DrawCircle(...);
DrawingHelper. Draw...(...);
DrawingHelper.CloseTrasaction();
I'm almost done now 🙂
Thank you!
Can't find what you're looking for? Ask the community or share your knowledge.