- 积分
- 36112
- 明经币
- 个
- 注册时间
- 2006-12-16
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
本帖最后由 qjchen 于 2013-10-20 19:31 编辑
前天
Cabinsummer提出了 http://bbs.mjtd.com/thread-91567-1-1.html
块内图元的拷贝问题,由于不是很熟悉lisp的块内操作,又想学习一下.Net的编法,于是借鉴了Kean的代码,
http://through-the-interface.typepad.com/through_the_interface/2010/06/changing-the-layer-of-an-entity-in-an-autocad-block-using-net.html
但为了历遍里面的实体和拷贝部分实体,就学习了一些.Net的Matrix函数和历遍
图片如下,代码如下,我的.Net水平挺低的,代码没有怎么优化,各位见笑了:)
不过对于不等比例的块,此程序是会报错的,可能得用.explode了。
DLL是可以netload的,命令是cnl
-
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.Geometry;
- namespace form1
- {
- public class KeanCopyFromBlock
- {
- [CommandMethod("CNL")]
- static public void ChangeNestedLayer()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- Transaction tr = doc.TransactionManager.StartTransaction();
- // 选择是所有块内实体原地拷贝还是选择部分块内实体拖动拷贝
- PromptKeywordOptions pko = new PromptKeywordOptions("\n是 1.选择所有块内实体原地拷贝 2.选择部分块内实体拖动拷贝: [1/2] ", "1, 2");
- PromptResult pr = ed.GetKeywords(pko);
- if (pr.Status != PromptStatus.OK)
- {
- ed.WriteMessage("\n*Canceled*\n");
- return;
- }
- using (tr)
- {
- // Loop until cancelled or completed
- PromptEntityOptions peo = new PromptEntityOptions("\n请选择一个块:");
- peo.SetRejectMessage("\n必须是一个块:");
- peo.AddAllowedClass(typeof(BlockReference), true);
- PromptEntityResult per = ed.GetEntity(peo);
- if (per.Status != PromptStatus.OK) return;
- BlockReference br = tr.GetObject(per.ObjectId, OpenMode.ForWrite) as BlockReference;
- switch (pr.StringResult)
- {
- case "1":
- BlockTableRecord btr = tr.GetObject(br.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
- foreach (ObjectId id in btr)
- {
- Entity Ent = tr.GetObject(id, OpenMode.ForRead) as Entity;
- if (Ent != null)
- {
- Entity tempEnt = qSubEntityClone(Ent, ed, db, doc, br);
- }
- }
- break;
- case "2":
- PromptNestedEntityResult rs;
- // Collection of our selected entities
- ObjectIdCollection ids = new ObjectIdCollection();
- rs = ed.GetNestedEntity("\n请选择需要拷贝的该块内的实体: ");
- if (rs.Status == PromptStatus.OK)
- {
- ids.Add(rs.ObjectId);
- HighlightSubEntity(doc, rs);
- }
- //while (rs.Status == PromptStatus.OK);
- if (ids.Count > 0)
- {
- for (int i = 0; i < ids.Count; i++)
- {
- Entity Ent = tr.GetObject(ids, OpenMode.ForWrite) as Entity;
- if (Ent != null)
- {
- Entity tempEnt = qSubEntityClone(Ent, ed, db, doc, br);
- PromptPointResult bpResult, dpResult;
- PromptPointOptions bpProps = new PromptPointOptions("\n拷贝起点:");
- bpProps.AllowNone = true;
- bpProps.UseBasePoint = false;
- bpResult = ed.GetPoint(bpProps);
- if (bpResult.Status != PromptStatus.OK)
- throw new System.Exception("\n用户取消.");
- PromptPointOptions dpProps = new PromptPointOptions("\n拷贝终点:");
- dpProps.AllowNone = true;
- dpProps.UseBasePoint = true;
- dpProps.BasePoint = bpResult.Value;
- dpResult = ed.GetPoint(dpProps);
- if (dpResult.Status != PromptStatus.OK)
- throw new System.Exception("\n用户取消.");
- Point3d pa = bpResult.Value;
- Point3d pb = dpResult.Value;
- qCopy(tempEnt, pa, pb);
- }
- }
- }
- break;
- default
- :
- break;
- }
- tr.Commit();
- // Regen clears highlighting and reflects the new layer
- ed.Regen();
- }
- }
- static private Entity qSubEntityClone(Entity Ent, Editor ed, Database db, Document doc, BlockReference br)
- {
- Transaction tr = doc.TransactionManager.StartTransaction();
- BlockTableRecord CurSpace = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
- LayerTable LayTbl = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
- LinetypeTable LtTbl = tr.GetObject(db.LinetypeTableId, OpenMode.ForRead) as LinetypeTable;
- Dictionary<ObjectId, Handle> Dict = new Dictionary<ObjectId, Handle>();
- object Obj = Ent.Clone();
- Entity tempEnt = Obj as Entity;
- if (tempEnt == null) return null;
- using (tr)
- {
- LayerTableRecord ltr = tr.GetObject(Ent.LayerId, OpenMode.ForRead) as LayerTableRecord;
- tempEnt.LayerId = Ent.LayerId;
- tempEnt.Color = Ent.EntityColor.ColorMethod == Autodesk.AutoCAD.Colors.ColorMethod.ByLayer ? ltr.Color : Ent.Color;
- tempEnt.LinetypeId = Ent.Linetype == "ByLayer" ? ltr.LinetypeObjectId : Ent.LinetypeId;
- tempEnt.TransformBy(br.BlockTransform);
- CurSpace.AppendEntity(tempEnt);
- tr.AddNewlyCreatedDBObject(tempEnt, true);
- //Dict.Add(tempEnt.ObjectId, ids.Handle);
- //下面这段是对块内块的,暂时我就不考虑了
- if (tempEnt is BlockReference)
- {
- BlockReference tempBlkRef = tempEnt as BlockReference;
- foreach (ObjectId attId in tempBlkRef.AttributeCollection)
- {
- Ent = tr.GetObject(attId, OpenMode.ForWrite) as Entity;
- Ent.Color = tempEnt.Color;
- //Ent.LayerId = tempLayId;
- Ent.Linetype = "Continuous";
- //Dict.Add(attId, attId.Handle);
- }
- }
- tr.Commit();
- }
- return tempEnt;
- }
- private static void HighlightSubEntity(
- Document doc, PromptNestedEntityResult rs
- )
- {
- // Extract relevant information from the prompt object
- ObjectId selId = rs.ObjectId;
- ObjectId[] objIds = rs.GetContainers();
- int len = objIds.Length;
- // Reverse the "containers" list
- ObjectId[] revIds = new ObjectId[len + 1];
- for (int i = 0; i < len; i++)
- {
- ObjectId id =
- (ObjectId)objIds.GetValue(len - i - 1);
- revIds.SetValue(id, i);
- }
- // Now add the selected entity to the end
- revIds.SetValue(selId, len);
- // Retrieve the sub-entity path for this entity
- SubentityId subEnt =
- new SubentityId(SubentityType.Null, 0);
- FullSubentityPath path = new FullSubentityPath(revIds, subEnt);
- // Open the outermost container, relying on the open
- // transaction...
- ObjectId id2 = (ObjectId)revIds.GetValue(0);
- Entity ent = id2.GetObject(OpenMode.ForRead) as Entity;
- // ... and highlight the nested entity
- if (ent != null)
- ent.Highlight(path, false);
- }
- private static void qCopy(Entity ent, Point3d p1, Point3d p2)
- {
- Vector3d vec = p2 - p1;
- Matrix3d mt = Matrix3d.Displacement(vec);
- ent.TransformBy(mt);
- }
- }
- }
此代码+dll
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
x
|