Civil 3d设置横断面图样式

时间:2023-03-08 23:02:44
Civil 3d设置横断面图样式

一位网友提出这样一个问题:

在使用SectionView.StyleName属性时,

会抛出异常:need to override property StyleName.

我测试的结果一样,

同时测试了StyleId

结果是类似的:need to override property StyleId().

于是我想通过COM方式来实现,

经过测试,

能够达到目的,

虽然绕了一些,

但总比不能实现要强一点点。

Civil 3d设置横断面图样式

测试的代码如下:

        [CommandMethod("MyGroup", "Tt101", CommandFlags.Modal)]
public void TestCommand1() // This method can have any name
{
Document doc = Application.DocumentManager.CurrentDocument;
Editor ed = doc.Editor;
CivilDocument cDoc = CivilApplication.ActiveDocument;
PromptEntityOptions peo = new PromptEntityOptions("\n拾取横断面");
peo.SetRejectMessage("\n选择横断面图");
peo.AddAllowedClass(typeof(SectionView),true);
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status==PromptStatus.OK)
{
try
{
//////using (Transaction tr = doc.TransactionManager.StartTransaction())
//////{
////// SectionView sv = per.ObjectId.GetObject(OpenMode.ForWrite) as SectionView;
////// //sv.StyleName = "测试样式"; //need to override property StyleName.
////// //var id = cDoc.Styles.SectionViewStyles["测试样式"];
////// //sv.StyleId = id; //need to override property StyleId().
////// tr.Commit();
//////}
//////////////////////////////////////////////////////////////////////////
IntPtr comIdPtr = per.ObjectId.OldIdPtr;
long comId = comIdPtr.ToInt64();
AeccSectionView asv = ComCivilDoc.ObjectIdToObject(comId);
var id = cDoc.Styles.SectionViewStyles["测试样式"];
AeccSectionViewStyle asvs= ComCivilDoc.ObjectIdToObject(id.OldId);//偷懒,试一下过时的方法还行不?
asv.set_Style(asvs);
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.Message);
}
} }

代码中的ComCivilDoc详见《AutoCAD Civil 3D .NET二次开发》第226页,

转换方法在第232页。

测试以上代码需要测试的dwg文件中具有名称为“测试样式”的横断面图样式。