- 积分
- 8888
- 明经币
- 个
- 注册时间
- 2012-8-7
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
本帖最后由 Real_King 于 2015-6-4 17:53 编辑
运行一次还好,第二次CAD就崩溃,自动关闭或APPLICATION 停止工作。百思不得其解,请各位老师指教!详细见代码及实验用图
由于自己找不到原因,所以说不出所以然,大概确定问题应该在第二个
using (Transaction trans = doc.TransactionManager.StartTransaction())的部分里(文字lable点化之后)
奇怪的是,有时去掉获取质心或获取自身地块编码的代码部分,就能多运行几次才崩溃,到底是什么原因呢?
代码:
public void SZSM()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
Database db = HostApplicationServices.WorkingDatabase;
#region 整体文字lable点化
try
{
TypedValue[] TXT_typevalues = new TypedValue[]
{
new TypedValue((int) DxfCode.LayerName, "地块编码,权利人,地类编码,四至" ),
new TypedValue ((int) DxfCode.Start, "TEXT")
};
SelectionFilter TXT_filter = new SelectionFilter(TXT_typevalues);
PromptSelectionResult TXT_psr = ed.SelectAll(TXT_filter);
SelectionSet TXT_ss = TXT_psr.Value;
using (Transaction trans = doc.TransactionManager.StartTransaction())
{
foreach (ObjectId TXT_id in TXT_ss.GetObjectIds())
{
Entity ent = (Entity)trans.GetObject(TXT_id, OpenMode.ForWrite);
DBText mark = (DBText)ent; mark.Height = 0.000000000001;
}
TXT_ss = null; ed.WriteMessage("处理相关文字...");
trans.Commit();
}
}
catch (SystemException ex)
{ MessageBox.Show("处理相关文字失败:\r\n"+ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); }
#endregion
#region 选择集过滤:承包地块
TypedValue[] CBDK_typevalues = new TypedValue[]
{
new TypedValue((int) DxfCode.LayerName, "承包地块" ),
new TypedValue ((int) DxfCode.Start, "LWPOLYLINE")
};
SelectionFilter CBDK_filter = new SelectionFilter(CBDK_typevalues);
PromptSelectionResult CBDK_psr = ed.SelectAll(CBDK_filter);
SelectionSet CBDK_ss = CBDK_psr.Value;
#endregion
using (Transaction trans = doc.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead); //以读方式打开块表.
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
foreach (ObjectId CBDK_id in CBDK_ss.GetObjectIds())//遍历每个承包地块
{
string SelfDKBM = "###";//自身地块编码
Polyline CBDK_PL = trans.GetObject(CBDK_id, OpenMode.ForWrite) as Polyline;
CBDK_PL.Elevation = 0; CBDK_PL.ColorIndex = 1;//自身承包地块变色
#region 建立缓冲区外围线
DBObjectCollection OffsetPLcc_FN = null; DBObjectCollection OffsetPLcc_FM = null;
if (CBDK_PL.Clockwise())//判断绘制方向
{ OffsetPLcc_FN = CBDK_PL.GetOffsetCurves(-3); OffsetPLcc_FM = CBDK_PL.GetOffsetCurves(-10); }
else { OffsetPLcc_FN = CBDK_PL.GetOffsetCurves(3); OffsetPLcc_FM = CBDK_PL.GetOffsetCurves(10); }
#endregion
#region 取得自身地块编码号
Point3dCollection SelfShapePoint = new Point3dCollection();
for (int i = 0; i < CBDK_PL.NumberOfVertices; i++)
{
SelfShapePoint.Add(CBDK_PL.GetPoint3dAt(i));
}
TypedValue[] SelfDKBM_values = new TypedValue[]
{
new TypedValue((int)DxfCode.LayerName, "地块编码"),
new TypedValue((int)DxfCode.Start, "TEXT")
};
SelectionFilter SelfDKBM_filter = new SelectionFilter(SelfDKBM_values);
PromptSelectionResult SelfDKBM_psr = ed.SelectCrossingPolygon(SelfShapePoint, SelfDKBM_filter);
SelectionSet SelfDKBM_ss = SelfDKBM_psr.Value;
if (SelfDKBM_ss.Count == 1)
{
ObjectId[] SelfDKBM_Obj = SelfDKBM_ss.GetObjectIds();
if (SelfDKBM_Obj[0] != null)
{
Entity SelfDKBM_ent = trans.GetObject(SelfDKBM_Obj[0], OpenMode.ForWrite) as Entity;
if (SelfDKBM_ent != null)
{
DBText DKBM = (DBText)SelfDKBM_ent;
SelfDKBM = DKBM.TextString.ToString();
}
}
}
else { SelfDKBM = "不确定地块编码"; }
#endregion
#region 扫描过程
for (int i = 0; i < OffsetPLcc_FN.Count; i++)
{
Polyline OffsetPL_FN = (Polyline)OffsetPLcc_FN;
OffsetPL_FN.Elevation = 0; OffsetPL_FN.Layer = "0";
btr.AppendEntity(OffsetPL_FN);
trans.AddNewlyCreatedDBObject(OffsetPL_FN, true);
//btr.AppendEntity((Entity)OffsetPLcc_FM);
//trans.AddNewlyCreatedDBObject((Entity)OffsetPLcc_FM, true);
}
#endregion
}
trans.Commit();
}
}//SZSM
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
x
|