|
前几天论坛QQ群里几位朋友想实现的问题,这里统一说下吧。通过闭合曲线生成面域的方法- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Geometry;
- [assembly: CommandClass(typeof(BimCad.CreataProfile))]
- namespace BimCad
- {
- class CreataProfile
- {
- [CommandMethod("LineToRegon")]
- public void LineToRegon()
- {
- Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- DBObjectCollection profile = Collection();
- Region region = Region(profile);
- ToModelSpace(region, db);
- }
- /// <summary>
- /// 将一个实体添加到当前空间
- /// </summary>
- /// <param name="ent">对象</param>
- /// <param name="db">数据库</param>
- /// <returns></returns>
- public static ObjectId ToModelSpace(Entity ent, Database db)
- {
- ObjectId id;
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- id = ((BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, false)).AppendEntity(ent);
- tr.AddNewlyCreatedDBObject(ent, true);
- tr.Commit();
- }
- return id;
- }
- /// <summary>
- /// 选择集合
- /// </summary>
- /// <returns></returns>
- public static DBObjectCollection Collection()
- {
- Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
- Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
- Entity entity = null;
- DBObjectCollection EntityCollection = new DBObjectCollection();
- PromptSelectionResult ents = ed.GetSelection();
- if (ents.Status == PromptStatus.OK)
- {
- using (Transaction transaction = db.TransactionManager.StartTransaction())
- {
- SelectionSet SS = ents.Value;
- foreach (ObjectId id in SS.GetObjectIds())
- {
- entity = (Entity)transaction.GetObject(id, OpenMode.ForWrite, true);
- if (entity != null)
- EntityCollection.Add(entity);
- }
- transaction.Commit();
- }
- }
- return EntityCollection;
- }
- /// <summary>
- /// 由线条集合创建面域
- /// </summary>
- /// <param name="curveSegments"></param>
- /// <returns></returns>
- public static Region Region(DBObjectCollection curveSegments)
- {
- return Autodesk.AutoCAD.DatabaseServices.Region.CreateFromCurves(curveSegments)[0] as Region;
- }
- }
- }
复制代码
|
|