反射学习
Civil 3D样例文件中关于样式比较的例子,
里面采用了反射,
快速的访问数据库中的每一种样式,
是一个很好的例子。
昨天自己折腾了快一天,
才完成的代码,
没想到在这个样例里居然都可以直接找到。
看来有时间应该把每一个样例都阅读一遍。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.Civil.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Windows;
using Autodesk.Civil.DatabaseServices.Styles;
using System.Reflection;
using Autodesk.AutoCAD.DatabaseServices;
using System.IO;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;
namespace CompareStyles {
///
<summary>
/// This example illustrates the Civil 3D styles API.
/// Styles are organized in a way that makes it easy to examine them
/// using .NET reflection. A Civil document contains all styles in a StylesRoot
/// object. This object contains collections of styles, and other
/// "root" objects for groups of related styles (such as TableStyles).
///
/// All collections of styles derive from StyleCollectionBase, and
/// all styles derive from StyleBase.
///
</summary>
public
class CompareStyles : IExtensionApplication {
private
static Transaction ts;
///
<summary>
/// Command to show our dialog for comparing the styles.
///
</summary>
[CommandMethod("CompareStyles")]
public
static
void ShowDlg () {
StyleFrm sf = new StyleFrm();
sf.Show();
}
///
<summary>
/// Gets the styles for the currently open document, puts the information
/// for each style into an StyleInfo object, and returns an ArrayList of
/// the StyleInfo objects.
///
</summary>
///
<returns></returns>
public
static ArrayList GetStyles () {
ArrayList styleList = new ArrayList();
using ( ts = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.
Database.TransactionManager.StartTransaction() ) {
CivilDocument CivilDoc = Autodesk.Civil.ApplicationServices.CivilApplication.ActiveDocument;
ListRoot(CivilDoc.Styles, styleList);
}
ts.Dispose();
return styleList;
}
///
<summary>
/// Looks at a "root" object for styles. Each root class contains a group of
/// collections (derived from StyleBaseCollection), or other style root objects.
///
</summary>
///
<param name="root">The style root object to look at.</param>
private
static
void ListRoot ( object root, ArrayList styleList ) {
// Get all the properties
Type objectType = root.GetType();
PropertyInfo[] properties = objectType.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance);
foreach ( PropertyInfo pf in properties ) {
// If it's a collection, let's iterate through it
if ( pf.PropertyType.ToString().Contains("Collection") ) {
// Debug.WriteLine(String.Format("Processing collection: {0}", pf.Name));
ListCollection(objectType, pf, root, styleList);
} else
if ( pf.PropertyType.ToString().Contains("Root") ) {
// Call ourselves recursively on this style root object
object root2 = objectType.InvokeMember(pf.Name,
BindingFlags.GetProperty, null, root, new
object[0]);
if ( root2.Equals(null) )
return;
ListRoot(root2, styleList);
} else
if ( pf.PropertyType.ToString().Contains("Default") ) {
// A default type, just use the name
} else {
// We're not sure what this is
}
}
}
///
<summary>
/// Iterates through a style collection (derived from StyleCollectionBase), and
/// prints out the name of each style it contains.
///
</summary>
///
<param name="objectType"></param>
///
<param name="pf"></param>
///
<param name="myStylesRoot"></param>
private
static
void ListCollection ( Type objectType, PropertyInfo pf, object myStylesRoot, ArrayList styleList ) {
object res = objectType.InvokeMember(pf.Name,
BindingFlags.GetProperty, null, myStylesRoot, new
object[0]);
if ( res.Equals(null) )
return;
StyleCollectionBase scBase = ( StyleCollectionBase )res;
foreach ( ObjectId sbid in scBase ) {
StyleBase stylebase = ts.GetObject(sbid, OpenMode.ForRead) as StyleBase;
// Add the style name and parameters to the list of all styles
StyleInfo styleinfo = new StyleInfo();
styleinfo.name = stylebase.Name;
styleinfo.type = stylebase.GetType().ToString();
// Get all the properties
Type styleType = stylebase.GetType();
PropertyInfo[] properties = styleType.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance);
foreach ( PropertyInfo stylePropInfo in properties ) {
object styleprop = null;
if ( stylePropInfo.Name.Contains("Application") || stylePropInfo.Name.Contains("Document") ) {
styleinfo.paramValues.Add(stylePropInfo.Name, "Not implemented");
return;
}
try {
styleprop = styleType.InvokeMember(stylePropInfo.Name,
BindingFlags.GetProperty, null, stylebase, new
object[0]);
// For object ID values, we need to get the target style base
// and record the name.
if ( styleprop.GetType().Name == "ObjectId" ) {
ObjectId objid = ( ObjectId )styleprop;
StyleBase proptarget = ts.GetObject(objid, OpenMode.ForRead) as StyleBase;
styleprop = proptarget.Name;
}
} catch ( System.Reflection.TargetInvocationException) {
// Should do something
styleprop = "NULL";
} catch ( System.Exception) {
styleprop = "NULL";
}
if ( !styleinfo.paramValues.ContainsKey(stylePropInfo.Name) ) {
styleinfo.paramValues.Add(stylePropInfo.Name, styleprop.ToString());
}
}
styleinfo.parent = pf.Name;
styleList.Add(styleinfo);
}
}
#region IExtensionApplication Members
public
void Initialize () { ShowDlg(); }
public
void Terminate () { }
#endregion
}
///
<summary>
/// A class to store style information
///
</summary>
public
class StyleInfo {
public String parent; // Name of the collection holding the style
public String name; // Name of the style
public String type; // Type of the style, not currently used
public Dictionary<String, String> paramValues; // dictionary of style parameters, name/value pairs
public StyleInfo () {
paramValues = new Dictionary<string, string>();
}
public String toString () {
String val = String.Format("\n\n*Parent: {0} Style: {1} (type {2})\n", parent, name, type);
foreach ( KeyValuePair<string, string> kp in paramValues ) {
val += String.Format(" Param: {0}: {1}\n", kp.Key, kp.Value);
}
return val;
}
}
}
标签:
Civil3d
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?