ObjectARX-CAD实体求空间交集

       最近在做一项CAD二次开发,就是根据一个行政单元,求在行政单元内的所有河流的面积。如果用ArcGIS来做,显然简单的多,但是用CAD来“滥竽充数”显然很不给力。下面介绍一下ObjectARX来空间求交集,多谢师弟的帮助。开发环境是VS2005。

       1. 首先创建一个类工程,添加5个比用引用,分别是:

            acdbmgd(位置在“C:/Program Files/AutoCAD 2006/acdbmgd.dll”)

            acmgd(位置在“C:/Program Files/AutoCAD 2006/acmgd.dll”)

    AutoCAD(COM组件库,别名:AutoCAD 2006 Type Library)

            AxDBLib(COM组件库,别名:AutoCAD/OjbectDBX Common 16.0 Type Library)

 

       2. 创建一个CAD实例类:

         using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using Autodesk.AutoCAD.Interop;//需要引用com namespace cadBoolOperator { #region CLASS AutoCADConnector(利用COM) public class AutoCADConnector : IDisposable { private AcadApplication _application; private bool _initialized; private bool _disposed; public AutoCADConnector() { try { _application = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.16"); } catch { try { _application = new AcadApplicationClass(); _initialized = true; } catch { throw; } } } ~AutoCADConnector() { Dispose(false); } public AcadApplication Application { get { return _application; } } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!this._disposed && _initialized) _application.Quit(); _disposed = true; } } #endregion }  

       3. 创建一个函数,将闭合的多义线(Polyline)转换为面(Region)

           /// <summary> /// 将闭合的多义线转化为面 /// </summary> /// <param name="pline"></param> /// <returns></returns> private static DATABASE.Region Convert2Region(Polyline pline) { Database AcadDB = HostApplicationServices.WorkingDatabase; try { using (Transaction trans = AcadDB.TransactionManager.StartTransaction()) { //BlockTable bt = (BlockTable)trans.GetObject(AcadDB.BlockTableId, OpenMode.ForRead, false); //BlockTableRecord btr = (BlockTableRecord)trans.GetObject(AcadDB.CurrentSpaceId, OpenMode.ForWrite, false); DBObjectCollection curveSegments = new DBObjectCollection(); curveSegments.Add(pline); DBObjectCollection regions = DATABASE.Region.CreateFromCurves(curveSegments); DATABASE.Region region = regions[0] as DATABASE.Region; if (region != null) { trans.Commit(); return region; } } } catch (Autodesk.AutoCAD.Runtime.Exception err)//如果多义线自相交或不在一个平面上 { //ACADUtilities.UtilityClass.ShowMessage("/nError in CreateRegion: " + err.ErrorStatus.ToString()); } catch { //ACADUtilities.UtilityClass.ShowMessage("/nError in CreateRegion/n"); } return null; } 

       4. 将两个封闭的多义线(Polyline)转换为面(Region)后,就可以求交集了:

           /// <summary> /// 计算两个面域公共部门面积(求交) /// </summary> /// <param name="r1"></param> /// <param name="r2"></param> /// <returns></returns> private static double calculate(Region r1, Region r2) { r1.BooleanOperation(BooleanOperationType.BoolIntersect, r2); return r1.Area; } 

你可能感兴趣的:(null,application,database,Class,library,BT)