Code Red > .NET

Does the .NET API have an equivalent to ActiveX AcadEntity class?

(1/3) > >>

pjm8765:
I'm writing what I hope will be one routine to remove a circle/line/polyline etc from model space.  These objects are temporary objects my commands use for various reasons, which I need to get rid of when I'm cleaning up at the end of each command.  Rather than have a separate routine for each class of object or iterating through the entities in model space, I am hoping that this rather useful class that ActiveX provides has an equivalent.

If not I'll simply carry on writing separate routines.

gile:
Hi,

You can use inheritance.
Circle, Line, Polyline classes are derived from Curve, which is drived from Entity, which is derived from DBObject which provide the Erase() method.
That means a DBObject instance, as any instance of the derived classes, can call the Erase() method.

pjm8765:
I've tried that and it results in an access violation.  Code :


--- Code: ---                easiBaseOutline = Utility.AddCircleToDrawing(insertPoint, 600, LayerNames.TemporaryEntity);

                //Ask for the output pipe
                outputPipe = GetOutputPipe();

                if (outputPipe == null)
                {
                    //ESC pressed, so stop the command
                    return;
                }

                //Check that the outlet pipe intersects with the EasiBase outline

                //Ask for the inlet pipes

                //Open the EasiBase form

            }
            catch (Exception ex)
            {
                MessageBox.Show("EasiBase.Create" + ex.Message, MessageHeadings.ExceptionError);
            }
            finally
            {
                if (easiBaseOutline != null)
                {
                    easiBaseOutline.Erase();
                }
            }

--- End code ---

The circle to be deleted is created by :


--- Code: ---        public static Circle AddCircleToDrawing(Point3d InsertPoint, double Radius, string LayerName)
        {
            Document thisDrawing;
            Circle newCircle = null;
            BlockTable blockTable;
            BlockTableRecord blockTableRecord;

            try
            {

                thisDrawing = GetDocument();

                if (thisDrawing == null)
                {
                    return newCircle;
                }

                // Start a transaction
                using (Transaction autoCADTransaction = thisDrawing.Database.TransactionManager.StartTransaction())
                {
                    // Open the Block table for read
                    blockTable = autoCADTransaction.GetObject(thisDrawing.Database.BlockTableId, OpenMode.ForRead) as BlockTable;

                    // Open the Block table record Model space for write
                    blockTableRecord = autoCADTransaction.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                    using (newCircle = new Circle())
                    {
                        // Create the circle entity
                        newCircle.Center = InsertPoint;
                        newCircle.Radius = Radius;
                        newCircle.Layer = LayerName;
                        newCircle.Visible = true;

                        // Add the new object to the block table record and the transaction
                        blockTableRecord.AppendEntity(newCircle);

                        autoCADTransaction.AddNewlyCreatedDBObject(newCircle, true);
                    }

                    // Save the new object to the database
                    autoCADTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Utility.AddCircleToDrawing" + ex.Message, MessageHeadings.ExceptionError);
            }

            return newCircle;
        }

--- End code ---

Which is a copy of a pretty vanilla routine from the Autodesk help.

I had assumed that deleting this object would require a database transaction, as the create routine does.

pjm8765:
To be sure all of the examples of deleting objects I find either involve using the ObjectId to find the object in the model, which I assume is slow. ....

Or, like this (http://docs.autodesk.com/ACD/2011/ENU/filesMDG/WS1a9193826455f5ff2566ffd511ff6f8c7ca-3eef.htm) involve creating the object and then deleting it in a single transaction....which is no good to me in the long run.

I could create one single big fat transaction that covers the entire command, but that would involve some painful double checking as and when users exit the command and is simply bad programming all round.

pjm8765:
I thought I'd go for the ObjectId route using this:


--- Code: ---        public void Erase(ObjectId id)
        {
            if (id.Database.TransactionManager.TopTransaction != null)
                id.GetObject(OpenMode.ForWrite).Erase();
            else
#pragma warning disable CS0618
                using (var obj = id.Open(OpenMode.ForWrite))
                    obj.Erase();
#pragma warning restore CS0618
        }

--- End code ---

Anyway, it turns out that the ObjectId and most of the attributes of my circle are generating an access violation.  So something is going wrong with my create routine passing back a reference to the newly created circle entity.

This is pretty basic programming practice i.e. create an object in a method and return for use and abuse.  I've noticed a lot of the Autodesk help examples create thingy's and then return void.

Please don't tell me I've got to create an entity and then go and find it again...or am I back to the idea of having one big fat transaction?

What is happening here?

Navigation

[0] Message Index

[#] Next page

Go to full version