一位網友提出這樣一個問題:
在使用SectionView.StyleName屬性時,
會拋出異常:need to override property StyleName.
我測試的結果一樣,
同時測試了StyleId
結果是類似的:need to override property StyleId().
於是我想通過COM方式來實現,
經過測試,
能夠達到目的,
雖然繞了一些,
但總比不能實現要強一點點。

測試的代碼如下:
[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文件中具有名稱為“測試樣式”的橫斷面圖樣式。
