乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 188|回复: 9

[原创]翔麟专集——实现类似CAD中的Block创建块义命令程序

[复制链接]

20

主题

73

帖子

3

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
153
发表于 2009-9-19 10:44:00 | 显示全部楼层 |阅读模式

d1ki0ndnavs.gif

d1ki0ndnavs.gif


  1. [CommandMethod("CreateBlock")]
  2. public void CreateBlock()
  3. {
  4.     Database db = HostApplicationServices.WorkingDatabase;
  5.     Editor ed = Autodesk..ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  6.     using (DocumentLock docLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
  7.     {
  8.         ObjectId blockId = ObjectId.Null;                   //用于返回所创建的块的对象Id
  9.         BlockTableRecord record = new BlockTableRecord();   //创建一个BlockTableRecord类的对象,表示所要创建的块
  10.         record.Name = "MyBlock";                            //设置块名  
  11.         record.Origin = new Point3d(0, 0, 0);               //设置块的基点
  12.         using (Transaction trans = db.TransactionManager.StartTransaction())
  13.         {
  14.             BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);//以写的方式打开块表
  15.             if (!bt.Has(record.Name))
  16.             {
  17.                 //选择对象
  18.                 PromptSelectionResult res = ed.GetSelection();
  19.                 if (res.Status == PromptStatus.OK)
  20.                 {
  21.                     foreach (ObjectId id in res.Value.GetObjectIds())
  22.                     {
  23.                         Entity ent = trans.GetObject(id, OpenMode.ForWrite) as Entity;
  24.                         Entity NewEnt = (Entity)ent.Clone();
  25.                         record.AppendEntity(NewEnt);
  26.                     }
  27.                 }
  28.                 bt.Add(record);                             //在块表中加入块
  29.                 trans.AddNewlyCreatedDBObject(record, true);//通知事务处理
  30.             }
  31.             else
  32.             {
  33.                 ed.WriteMessage("此图块名已存在,请检查!");
  34.             }
  35.             trans.Commit();
  36.         }
  37.     }
  38. }

以下是Vs2008-CAD2010程序源文件:

本帖以下内容被隐藏保护;需要你回复后,才能看到!

游客,如果您要查看本帖隐藏内容请回复
回复

使用道具 举报

7

主题

55

帖子

5

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
83
发表于 2009-10-1 19:03:00 | 显示全部楼层
很好.这个功能.我已经有了.看晚了点了.
回复

使用道具 举报

22

主题

99

帖子

2

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
187
发表于 2010-11-17 10:32:00 | 显示全部楼层
Cool!!
回复

使用道具 举报

84

主题

543

帖子

12

银币

中流砥柱

Rank: 25

铜币
886
发表于 2010-12-9 20:22:00 | 显示全部楼层
前几天还在为此功能郁闷呢,不过现在狐哥的帮助下已解决了...
回复

使用道具 举报

2

主题

7

帖子

2

银币

初来乍到

Rank: 1

铜币
15
发表于 2010-12-15 19:39:00 | 显示全部楼层
很不错。但有一个问题就是。创建块后,能不能把用来创建块的对像也一起转换成块呢。因为平时我们选择对象创建块后。选择的那些对象就会同时转换成块了。
回复

使用道具 举报

9

主题

100

帖子

7

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
136
发表于 2010-12-24 15:03:00 | 显示全部楼层
回复
把这几句
foreach (ObjectId id in res.Value.GetObjectIds())
                    {
                        Entity ent = trans.GetObject(id, OpenMode.ForWrite) as Entity;
                        Entity NewEnt = (Entity)ent.Clone();
                        record.AppendEntity(NewEnt);
                    }
换成
record.AssumeOwnershipOf( new ObjectIdCollection(res.Value.GetObjectIds()));
回复

使用道具 举报

rwc

0

主题

1

帖子

1

银币

初来乍到

Rank: 1

铜币
1
发表于 2010-12-30 13:25:00 | 显示全部楼层
学习了,不错
回复

使用道具 举报

10

主题

31

帖子

4

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
71
发表于 2011-1-2 21:18:00 | 显示全部楼层
这个不能指定插入点
回复

使用道具 举报

1

主题

12

帖子

3

银币

初来乍到

Rank: 1

铜币
16
发表于 2018-7-18 09:05:00 | 显示全部楼层
2012版测试无法通过,需要改为下面代码:using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
namespace CollectionCreation
{
    public class Commands
    {
        [CommandMethod("CB")]
        public void CreateBlock()
        {
            Document doc =
              Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            Transaction tr =
              db.TransactionManager.StartTransaction();
            using (tr)
            {
                // Get the block table from the drawing
                BlockTable bt =
                  (BlockTable)tr.GetObject(
                    db.BlockTableId,
                    OpenMode.ForRead
                  );
                // Check the block name, to see whether it's
                // already in use
                PromptStringOptions pso =
                  new PromptStringOptions(
                    "\nEnter new block name: "
                  );
                pso.AllowSpaces = true;
                // A variable for the block's name
                string blkName = "";
                do
                {
                    PromptResult pr = ed.GetString(pso);
                    // Just return if the user cancelled
                    // (will abort the transaction as we drop out of the using
                    // statement's scope)
                    if (pr.Status != PromptStatus.OK)
                        return;
                    try
                    {
                        // Validate the provided symbol table name
                        SymbolUtilityServices.ValidateSymbolName(
                          pr.StringResult,
                          false
                        );
                        // Only set the block name if it isn't in use
                        if (bt.Has(pr.StringResult))
                            ed.WriteMessage(
                              "\nA block with this name already exists."
                            );
                        else
                            blkName = pr.StringResult;
                    }
                    catch
                    {
                        // An exception has been thrown, indicating the
                        // name is invalid
                        ed.WriteMessage(
                          "\nInvalid block name."
                        );
                    }
                } while (blkName == "");
                // Create our new block table record...
                BlockTableRecord btr = new BlockTableRecord();
                // ... and set its properties
                btr.Name = blkName;
                // Add the new block to the block table
                bt.UpgradeOpen();
                ObjectId btrId = bt.Add(btr);
                tr.AddNewlyCreatedDBObject(btr, true);
                // Add some lines to the block to form a square
                // (the entities belong directly to the block)
                DBObjectCollection ents = SquareOfLines(5);
                foreach (Entity ent in ents)
                {
                    btr.AppendEntity(ent);
                    tr.AddNewlyCreatedDBObject(ent, true);
                }
                // Add a block reference to the model space
                BlockTableRecord ms =
                  (BlockTableRecord)tr.GetObject(
                    bt[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite
                  );
                BlockReference br =
                  new BlockReference(Point3d.Origin, btrId);
                ms.AppendEntity(br);
                tr.AddNewlyCreatedDBObject(br, true);
                // Commit the transaction
                tr.Commit();
                // Report what we've done
                ed.WriteMessage(
                  "\nCreated block named \"{0}\" containing {1} entities.",
                  blkName, ents.Count
                );
            }
        }
        private DBObjectCollection SquareOfLines(double size)
        {
            // A function to generate a set of entities for our block
            DBObjectCollection ents = new DBObjectCollection();
            Point3d[] pts =
          { new Point3d(-size, -size, 0),
            new Point3d(size, -size, 0),
            new Point3d(size, size, 0),
            new Point3d(-size, size, 0)
          };
            int max = pts.GetUpperBound(0);
            for (int i = 0; i <= max; i++)
            {
                int j = (i == max ? 0 : i + 1);
                Line ln = new Line(pts[i], pts[j]);
                ents.Add(ln);
            }
            return ents;
        }
    }
}
回复

使用道具 举报

7

主题

18

帖子

6

银币

初来乍到

Rank: 1

铜币
46
发表于 2019-10-8 15:04:00 | 显示全部楼层
这个是VBA吗
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-13 02:13 , Processed in 0.702340 second(s), 88 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表