马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
本帖最后由 csharp 于 2014-5-29 07:53 编辑
http://spiderinnet1.typepad.com/blog/2014/02/autocad-net-add-entity-in-model-space-to-block.html
In this article, let us see how to select an entity in the model space either WCS or UCS and append it to a particular block referenced by an INSERT (block reference), which is picked at the same time, maintaining the same relative position and rotation.
- [CommandMethod("AddEntToBlock")]
- public static void AddEntToBlock_Method()
- {
- Database db = HostApplicationServices.WorkingDatabase;
- Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
-
- try
- {
- PromptEntityResult prEntRes1 = ed.GetEntity("Select an entity: ");
- PromptEntityResult prEntRes2 = ed.GetEntity("Select a block reference (INSERT) to add the entity to: ");
-
- if (prEntRes1.Status == PromptStatus.OK && prEntRes2.Status == PromptStatus.OK)
- {
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- Entity ent = tr.GetObject(prEntRes1.ObjectId, OpenMode.ForWrite) as Entity;
- BlockReference insert = tr.GetObject(prEntRes2.ObjectId, OpenMode.ForRead) as BlockReference;
-
- if(insert == null)
- {
- ed.WriteMessage("\nThe second selected is not a block reference (INSERT).");
- return;
- }
-
- BlockTableRecord hostBlk = hostBlk = tr.GetObject(insert.BlockTableRecord, OpenMode.ForWrite) as BlockTableRecord;
-
- Matrix3d mat = insert.BlockTransform.Inverse();
-
- Entity clone = ent.Clone() as Entity;
- clone.TransformBy(mat);
- hostBlk.AppendEntity(clone);
- tr.AddNewlyCreatedDBObject(clone, true);
-
- ent.Erase();
-
- tr.Commit();
- }
- }
- }
- catch (System.Exception ex)
- {
- ed.WriteMessage(ex.ToString());
- }
- }
If we insert a sample block into the current drawing, draw a standalone entity such as circle in red, run the command, pick the entity and the INSERT (block reference) in turn, the entity (red circle here) will be moved from the model space regardless of WCS or UCS to the block space.
AddEntToBlock
Next time, when the same block is inserted, we will notice it has the red circle nicely added. Once again, the block can be any one defined in the current database; the block reference can have any insertion point and rotation angle; the entity and the INSERT can be in WCS or UCS; the entity can be of any type and can be anywhere as well.
Some readers may have noticed that the transformation Matrix3d does not seem to take into the current UCS, which is the case as demonstrated, in the code, but the command nicely handles both UCS and WCS. If wonder why, please keep on reading. We will reveal it soon.
|