.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Copy object inside same drawing... problems

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
C_Witt
1191 Views, 5 Replies

Copy object inside same drawing... problems

I've been using this method for "copying" objects from one point to another..

 

Dim EntityCopy As Entity = EntitySource.GetTransformedCopy(Matrix3d.Displacement(startPT.GetVectorTo(EndPT)))

 

But have run into a few glitches, not program stopping glitches just annoying ones..

 

With polylines, not all the properties are copied, one for example is the "global width".  it gets re-set to 0.

 

Problem is with dynamic blocks..  the above method creates a "dumb" version of the block rather than a functional dynamic version of the block..

 

Can anyone show me a way to fix this, or an alternate method for copying objects from point a to point b?

5 REPLIES 5
Message 2 of 6
Hallex
in reply to: C_Witt

In that case as for  Tony Tanzillo's recomendation I wouldt use

DeepCloneObjects method of DataBase instead

Try this quicky

No error trapping in there, do it by yourself

    <CommandMethod("como")> _
    Public Shared Sub CopyMove()
        Dim doc As Document = acApp.DocumentManager.MdiActiveDocument
        Dim db As Database = doc.Database
        Dim ed As Editor = doc.Editor

        Dim peo As New PromptEntityOptions(vbLf & "Select object:")

        Dim per As PromptEntityResult = ed.GetEntity(peo)

        If per.Status <> PromptStatus.OK Then

            Return
        End If
        Dim startPT As Point3d = ed.GetPoint(vbCr & "Pick a point from: ").Value
        Dim EndPT As Point3d = ed.GetPoint(vbCr & "Pick a point to: ").Value
        Using tr As Transaction = db.TransactionManager.StartTransaction()
            Dim btr As BlockTableRecord = CType(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)

            Dim sourceId As ObjectId = per.ObjectId

            Dim EntitySource As Entity = CType(tr.GetObject(sourceId, OpenMode.ForRead), Entity)
            Dim ids As New ObjectIdCollection()
            ids.Add(sourceId)
            db.DeepCloneObjects(ids, db.CurrentSpaceId, New IdMapping, False)
            Dim EntityCopy As Entity = CType(tr.GetObject(ids(0), OpenMode.ForRead), Entity)
            EntityCopy.UpgradeOpen()
            EntityCopy.TransformBy(Matrix3d.Displacement(startPT.GetVectorTo(EndPT)))
            EntityCopy.ColorIndex = 2

            tr.Commit()
        End Using

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 3 of 6
C_Witt
in reply to: C_Witt

Thank you, that is perfect. 🙂

Message 4 of 6
Hallex
in reply to: C_Witt

You're welcome 🙂

Glad to help

Regards,

 

Oleg

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 5 of 6
MRiemenCAD
in reply to: Hallex

Working with making blocks from existing entities in a file. I've run into problems with iterating through the entities within a selection set and writing them to my new blocktablerecord. How do I take those items from my selection set and add them to this new block? All thoughts and help are appreciated. 

Message 6 of 6
BKSpurgeon
in reply to: MRiemenCAD

For the benefit future readers:

 

Just create a new block table record rather than adding it to the model space block table record. I think i know what i'm doing, but i'll let the more experienced autocad developers tweak the code. This is some quick and dirty code which i previously wrote.

 

I suspect that the following is a shallow copy though - change to a deep one as you see fit.

 

      private static void CloneAndTrasnformTheObjects(PromptPointResult PPRbasePoint, PromptPointResult PPRdestinationPoint, SelectionSet ss)
        {
            // Get the transformation vector to apply to all cloned objects
            Point3d pointBase = PPRbasePoint.Value;
            Point3d pointDestination = PPRdestinationPoint.Value;
            Vector3d transformationVector = pointBase.GetVectorTo(pointDestination);

            // get the entites which we want to clone
            ObjectIdCollection entitiesToCopy = new ObjectIdCollection(ss.GetObjectIds());

            //set up the document/database/editor 
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            using (DocumentLock docLock = doc.LockDocument())
            {
                using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
                {

                    // clone the following objects
                    foreach (ObjectId id in entitiesToCopy)
                    {
                        BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                        BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                        Entity entityOriginal = tr.GetObject(id, OpenMode.ForRead) as Entity;
                        Entity entityClone = entityOriginal.Clone() as Entity;

                        // ensuring we have no null values
                        if (entityClone != null && entityOriginal != null && bt != null && btr != null)
                        {
                            entityClone.TransformBy(Matrix3d.Displacement(transformationVector));
                            btr.AppendEntity(entityClone);
                            tr.AddNewlyCreatedDBObject(entityClone, true);
                        }
                    }
                    tr.Commit();

                } 
            }
        }   

 

rgds

 

BK

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost