Ctrl-Z behavior

Ctrl-Z behavior

alain.holloway
Contributor Contributor
1,060 Views
10 Replies
Message 1 of 11

Ctrl-Z behavior

alain.holloway
Contributor
Contributor

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

 

 

0 Likes
Accepted solutions (1)
1,061 Views
10 Replies
Replies (10)
Message 2 of 11

ActivistInvestor
Mentor
Mentor

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.

0 Likes
Message 3 of 11

alain.holloway
Contributor
Contributor

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

0 Likes
Message 4 of 11

ActivistInvestor
Mentor
Mentor

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.

 

 

0 Likes
Message 5 of 11

alain.holloway
Contributor
Contributor
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

0 Likes
Message 6 of 11

ActivistInvestor
Mentor
Mentor

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.

0 Likes
Message 7 of 11

alain.holloway
Contributor
Contributor

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

0 Likes
Message 8 of 11

ActivistInvestor
Mentor
Mentor
Accepted solution

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

0 Likes
Message 9 of 11

alain.holloway
Contributor
Contributor

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

0 Likes
Message 10 of 11

ActivistInvestor
Mentor
Mentor

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.

0 Likes
Message 11 of 11

alain.holloway
Contributor
Contributor

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!

 

0 Likes