shers 发表于 2015-6-30 13:39:45

在同一图形中复制块

您好,
我正在尝试复制在side数据库中打开的图形中的特定块,并将其以相等的距离粘贴n次。这是我的代码:

try
            {
                using (db)
                {
                  db.ReadDwgFile(@"-------.dwg", FileOpenMode.OpenForReadAndAllShare, false, null);
                  db.CloseInput(true);
                  using (Transaction tr = db.TransactionManager.StartTransaction())
                  {
                        BlockReference br;
                        BlockTableRecord btr = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
                        foreach (ObjectId id in btr)
                        {
                            Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
                            if (ent is Line)
                            {
                              Line line = (Line)ent;
                              if (line.Handle.ToString() == "3D5")
                              {
                                    Point3d linePt = new Point3d();
                                    linePt = line.StartPoint;
                                    ed.WriteMessage(linePt.ToString());
                                    
                                    ObjectId brId = ObjectId.Null;
                                    string blockFile = @"--------.dwg";
                                    string blockName = Path.GetFileNameWithoutExtension(blockFile);
                                    
                                    using (Database tempDb =new Database(false, true))
                                    {
                                        tempDb.ReadDwgFile(blockFile, FileShare.Read, true, null);
                                        brId = db.Insert(blockName, tempDb, false);
                                        tempDb.Dispose();
                                     br = new BlockReference(linePt, brId);
                                        //tr.AddNewlyCreatedDBObject(br, true);
                                    }
                              }
                            }
                        }
                        tr.Commit();
                  }
                  db.SaveAs(@"----------.dwg", DwgVersion.Current);
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                ed.WriteMessage(ex.ToString());
            }

不幸的是,它不起作用。请帮忙。
谢谢
**** Hidden Message *****

MexicanCustard 发表于 2015-7-1 07:31:33

您必须将新的块引用添加到模型空间。

shers 发表于 2015-7-3 00:17:15

谢谢你的回复,我现在有了新的代码,这就是它,它将新的块引用添加到数据库中。   使用(Database db = new Database(false,true))。
{。
dbReadDwgFile(@" - y.dwg ",FileOpenMode。OpenForReadAndAllShare,false,null);。
dbCloseInput(真);。

使用(事务tr = db,transaction manager . start transaction())。
{。
块表bt =(块表)tr,GetObject(db,BlockTableId,OpenMode。for read);。

BlockTableRecord Bt rec =(block tablerecord)tr,GetObject(bt,开放模式。for read);。

foreach(btrec中的ObjectId id)。
{。
实体ent =(实体)tr,GetObject(id,OpenMode。for read);。

if (ent是BlockReference)。
{。
block reference bref =(block reference)trGetObject(ent,ObjectId,OpenMode。for read);。

if (bref,Name == "XYZ")。
{。

btrec,append entity(bref);。
trAddNewlyCreatedDBObject(bref,true);。
}。
dbtransaction manager . QueueForGraphicsFlush();。
}。
}。

trcommit();。
}。
db1,另存为(@"1.dwg ",DwgVersion。当前);。
}。
}。
catch(系统,例外情况,例如)。
{。
消息框,显示(例如,ToString());。
}。
我得到的错误是eAlreadyInDb,请帮帮忙,谢谢编辑:kdub -> code=csharp。

gile 发表于 2015-7-3 02:35:37

您好,
您必须创建找到的块参照(已在数据库中)的副本,并将该副本添加到数据库中
在这里看一看。

shers 发表于 2015-7-3 04:13:39

您提供的链接仅适用于实体。但我正在寻找块引用,其中我必须考虑插入点和名称。
谢谢

gile 发表于 2015-7-3 07:56:41


您是否对块引用(即实体)尝试过这种方式?如果是的话,你能提供你的测试代码吗?

shers 发表于 2015-7-4 02:16:20


Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead);

                            if (ent is BlockReference)
                            {
                              BlockReference bref = (BlockReference)tr.GetObject(ent.ObjectId, OpenMode.ForRead);

这是你的意思吗?它在上面的代码中。

gile 发表于 2015-7-4 04:51:38

尝试下面的CopyBlock()方法,它使用我提供的链接中显示的克隆过程,它需要3个参数:要复制的一些块引用应该插入并必须复制的数据库,这可以是一个辅助数据库或活动文档数据库(这使测试更容易);。
要搜索和复制的块的名称;。
要应用于复制的块重复引用的转换矩阵(Matrix3d实例)   private void copy block(Database db,string blockName,Matrix3d xform)。
{。
使用(事务tr = db,transaction manager . start transaction())。
{。
块表记录模型空间=(块表记录)tr,GetObject(。
符号实用服务,GetBlockModelSpaceId(db),OpenMode。for write);。
rx class blockRefClass = rx class,GetClass(type of(block reference));。
Listclones = new List ();。
foreach(模型空间中的ObjectId id)。
{。
如果(id,object class = = blockRefClass)。
{。
块引用br =(块引用)tr,GetObject(id,OpenMode。for read);。
如果(br,Name == blockName)。
{。
克隆人,Add((BlockReference)br,clone());。
}。
}。
}。
foreach(克隆中的块引用克隆)。
{。
克隆,transform by(xform);。
模型空间,AppendEntity(克隆);。
trAddNewlyCreatedDBObject(clone,true);。
}。
trcommit();。
}。
}调用示例(复制“XYZ”引用并将它们沿X轴移动15个单位):   数据库db = HostApplicationServices,工作数据库;。
Matrix3d xform = Matrix3d,位移(新Vector3d(15.0,00.0,0.0));。
CopyBlock(db,“XYZ”,xform);。

shers 发表于 2015-7-4 05:29:01

太棒了!!这正是我想要的。非常感谢!!你救了我的命。
我还想在第二次添加时在块上附加一行。我会努力并分享代码。
谢谢
页: [1]
查看完整版本: 在同一图形中复制块