视图部件对象DrawingComponent

从特征树上可以看到,每个带引用的视图上都引用了一个模型实例,这个模型实例可以是一个零件,也可以是一个装配体。

如下图所示,同样一个模型或装配体能同时被好几个视图以不同的实例号进行引用。这样我们就能对不同的视图中相同的部件进行一些不同的设置,如线型设置,隐藏显示部件等。

image.png

本文我们就来介绍下这个被视图引用的模型对象DrawingComponent。

DrawingComponent对象可以看作是零部件,装配体与工程图进行数据交互的桥梁。故可以通过工程图中的DrawingComponent间接地直接获得对应零件或装配体的文档对象,进而获取更详细的零部件数据。

实例1:获取DrawingComponent实例

获取DrawingComponent最直接的方式就是通过视图对象View实例获得。

代码示例:

public static void GetViewComp(ModelDoc2 SwDoc, string ViewName)
 {
     Feature SwFeat = ((DrawingDoc)SwDoc).FeatureByName(ViewName);
     View SwView = SwFeat.GetSpecificFeature2();
     DrawingComponent SwDrawComp = SwView.RootDrawingComponent;
     StringBuilder Sb = new StringBuilder("");
     Sb.Append("视图部件名:" + SwDrawComp.Name + "\r\n");
     Sb.Append("所属视图名:" + SwDrawComp.View.Name + "\r\n");
     Sb.Append("部件名:" + SwDrawComp.Component.Name2 + "\r\n");
     Sb.Append("子部件数量:" + SwDrawComp.GetChildrenCount().ToString().Trim() + "\r\n");//直接关联的顶层部件
     System.Windows.MessageBox.Show(Sb.ToString().Trim());
 }

执行效果:

image.png

实例分析:

主要通过View.RootDrawingComponent属性,获得视图的根模型对象

实例2:获取DrawingComponent对应的模型信息

代码示例:

public static void GetViewCompInfo(ModelDoc2 SwDoc, string ViewName, string DocCuspName)
{
      Feature SwFeat = ((DrawingDoc)SwDoc).FeatureByName(ViewName);
      View SwView = SwFeat.GetSpecificFeature2();
      DrawingComponent SwDrawComp = SwView.RootDrawingComponent;
      StringBuilder Sb = new StringBuilder("");
      Component2 SwComp = SwDrawComp.Component;
      ModelDoc2 CompDoc = SwComp.GetModelDoc2();
      Sb.Append(SwDrawComp.Name + ":" + CompDoc.Extension.CustomPropertyManager[""].Get(DocCuspName).ToString().Trim() + "\r\n");
      object[] ObjDrawComps = SwDrawComp.GetChildren();
      foreach (object ObjDrawComp in ObjDrawComps)
      {
          SwComp = ((DrawingComponent)ObjDrawComp).Component;
          CompDoc = SwComp.GetModelDoc2();
          Sb.Append(((DrawingComponent)ObjDrawComp).Name + ":" + CompDoc.Extension.CustomPropertyManager[""].Get(DocCuspName).ToString().Trim() + "\r\n");
      }
      System.Windows.MessageBox.Show(Sb.ToString().Trim());
}

执行效果:

image.png

实例分析:

主要通过DrawingComponent.Component属性获得Component对象,并进而得到零部件的文档对象ModelDoc2

实例3:设置视图中部件的线型表达

由于DrawingComponent是一个视图引用的实例,故通过其属性即可直接设置部件在对应视图中的线型,显示隐藏等信息

代码示例:

//选中特征树中的部件
public static void SetViewCompLine(ModelDoc2 SwDoc)
{
      SelectionMgr SwSelMrg = SwDoc.SelectionManager;
      DrawingComponent SwDrawComp = SwSelMrg.GetSelectedObjectsComponent4(1, 0);//需要选中特征树中的部件,不能在视图中直接选中部件
      if (SwDrawComp == null)
      {
           System.Windows.MessageBox.Show("请在特征树中选中部件!");
           return;
      }
      SwDrawComp.Style = (int)swLineStyles_e.swLineCENTER;
      SwDrawComp.Width = (int)swLineWeights_e.swLW_THICK6;
      System.Windows.MessageBox.Show("部件" + SwDrawComp.Name + ",线性设置成功!");
 }

执行效果:

image.png

如下图为本文的示例程序,源码可上我的Github下载。操作步骤可见文章《公众号源码Github分享库》 , 实例序号22

image.png

你可能感兴趣的:(视图部件对象DrawingComponent)