代码加载后多次运行会导致CAD崩溃,百思不得其解
本帖最后由 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, 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 != null)
{
Entity SelfDKBM_ent = trans.GetObject(SelfDKBM_Obj, 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
selectall也可能为null ivde 发表于 2015-5-3 21:23 static/image/common/back.gif
selectall也可能为null
兄弟是说,之后的运行中,SelectAll可能返回null?但是不应该啊,图形都是ZoomE的 Real_King 发表于 2015-5-3 21:45
兄弟是说,之后的运行中,SelectAll可能返回null?但是不应该啊,图形都是ZoomE的
选择后修改了,再选择就不一定有选集 用com反射获取质心试试
另外 我一般把选择集写在事务的外面
选择集的返回状态最好也要做判断 本帖最后由 ivde 于 2015-5-4 07:51 编辑
Polyline绘图不规范的时候不一定能生成 Region,可能为 null
避免崩溃方法就是用 try ... catch 结构,否则就多增加 if 判断
try
{
myRegionColl = Region.CreateFromCurves(acDBObjColl);
Region acRegion = myRegionColl as Region;
...
}
cathc {}
or
myRegionColl = Region.CreateFromCurves(acDBObjColl);
if (myRegionColl.count != 0 && myRegionColl != Region.IsNull)
{
Region acRegion = myRegionColl as Region;
....
页:
[1]