Code Red > .NET

Selection sets

(1/3) > >>

Humbertogo:
How to select a object  in one drawing and pass to another drawing?


            Dim acadDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim db As Database = acadDoc.Database
            Dim ed As Editor = acadDoc.Editor

            Dim peo As New PromptEntityOptions(vbLf & "Select source block reference: ")
            peo.SetRejectMessage(vbLf & "Entity is not a block.")
            peo.AddAllowedClass(GetType(BlockReference), False)

            Dim per As PromptEntityResult = ed.GetEntity(peo)

            If per.Status <> PromptStatus.OK Then
                Return
            End If



MexicanCustard:
If you're just trying to copy objects from one dwg to another look at WblockCloneObjects.

Humbertogo:
I just trying to select one object for example select dynamic bock get properties and pass to another Drawing

fixo:

--- Quote from: Humbertogo on July 11, 2012, 06:23:56 am ---I just trying to select one object for example select dynamic bock get properties and pass to another Drawing

--- End quote ---
Try this quick example, just change file name and block name to your suit,
I didn't test this code on dynamic block though


--- Code: ---       [CommandMethod("copyblock", CommandFlags.Session | CommandFlags.Modal)]
        public static void DynBlockCopyTest()
        {
            ObjectId btrid = ObjectId.Null;
            // set full path of container file, block name
            InsertDynBlockFromFile(@"C:\Test\WorkingDrawing.dwg", "IMP");
        }



        public static void InsertDynBlockFromFile(string sourcefile, string blkName)
        {

            ObjectIdCollection ids = new ObjectIdCollection();

            // Find the file containing all the blocks

            if (!File.Exists(sourcefile))
            {
                MessageBox.Show("Could not find file !");

                return;

            }

            DocumentCollection dm = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;

            ObjectId sourceid = ObjectId.Null;

            Document doc = dm.MdiActiveDocument;

            Database db = doc.Database;

            Editor ed = doc.Editor;

            try
            {

                Document sourcedoc = dm.Open(sourcefile, false);

                // Needed for source document since the source database has to be active.

                dm.MdiActiveDocument = sourcedoc;

                Editor sourced = sourcedoc.Editor;

                Database sourcedb = sourcedoc.Database;

                PromptEntityOptions peo = new PromptEntityOptions("\nSelect dynamic block: ");

                peo.SetRejectMessage("\nMight be of type BlockReference");

                peo.AddAllowedClass(typeof(BlockReference), true);

                PromptEntityResult res = sourced.GetEntity(peo);

                if (res.Status != PromptStatus.OK) return;

                using (Transaction sourcetr = sourcedb.TransactionManager.StartTransaction())
                {
                    BlockTable sourcebt = null;

                    sourcebt = sourcetr.GetObject(sourcedb.BlockTableId, OpenMode.ForRead) as BlockTable;

                    if (!sourcebt.Has(blkName))
                    {
                        MessageBox.Show("Block not found !");

                        return;

                    }

                    // Find the ObjectId of the block we are interested in.

                    sourceid = res.ObjectId;

                    ids.Add(sourceid);

                }


                using (DocumentLock doclock = doc.LockDocument())
                {
                   
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        BlockTable destbt = null;

                        destbt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                        BlockTableRecord destbtr = null;

                        ObjectId destbtrid = destbt[BlockTableRecord.ModelSpace];

                        destbtr = tr.GetObject(destbtrid, OpenMode.ForWrite) as BlockTableRecord;

                        // WBlockClone the BlockTable record to the current database

                        // Needed for document since the destination database has to be active.

                        dm.MdiActiveDocument = doc;

                        // ObjectId of the btr that we want to clone

                        IdMapping idmap = new IdMapping();

                        db.WblockCloneObjects(ids, destbtrid, idmap, DuplicateRecordCloning.Ignore, false);

                        // Here you may want to get inserted block and modify its properies and attributes

                        // Close source file without changes

                        sourcedoc.CloseAndDiscard();

                        tr.Commit();
                    }

                }
            }

            catch (System.Exception ex)
            {
                Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(string.Format("\n{0}\n{1}", ex.Message, ex.StackTrace));
            }
        }
--- End code ---

~'J'~

Humbertogo:
Thanks

Navigation

[0] Message Index

[#] Next page

Go to full version