C# SolidWorks 二次开发 API---各种遍历对象

已经有快一个月没有更新博客了,一方面是二宝出生了,休了半个月假。再一个就是实在是太忙了。
这篇遍历的已经计划很久了,一直在草稿状态。
在实际项目中,我们有时候无法通过一些名字或者信息来查找所要的对象,最快的方式就是通过遍历。然后再去通过细节来找到想要的数据。

  • 遍历图块
       ///这是根据名字来遍历图块的定义
       public static SketchBlockDefinition GetBlockInstenseByBlockName(string blockName)
		{
			
			SwApp = ConnectToSolidWorks();

			var swModel = (ModelDoc2)SwApp.ActiveDoc;

			var skMgr = swModel.SketchManager;

			var vBlocks = (object[])skMgr.GetSketchBlockDefinitions();

			if (vBlocks == null)
			{
				return null;
			}
			else
			{
				foreach (var item in vBlocks)
				{
					var blockDef = (SketchBlockDefinition)item;
					var fBlock = blockDef.GetFeature();

					if (fBlock.Name == blockName)
					{
						return blockDef;
					}
				}
			}

			return null;
		}
  • 遍历零件特征
	     	var swSelMgr = (SelectionMgr)swModel.SelectionManager;
            var swFeat = (Feature)swModel.FirstFeature();
     
            while ((swFeat != null))
            {              
                var swSubFeat = (Feature)swFeat.GetFirstSubFeature();

                while ((swSubFeat != null))
                {
                    Debug.Print(swSubFeat.Name.ToString());                    
                    swSubFeat = (Feature)swSubFeat.GetNextSubFeature();
                }

                swFeat = (Feature)swFeat.GetNextFeature();
            }
  • 遍历面上的边
		    swModel = (ModelDoc2)swApp.ActiveDoc;
            swModelExt = swModel.Extension;
            swSelMgr = (SelectionMgr)swModel.SelectionManager;
            swFeature = (Feature)swSelMgr.GetSelectedObject6(1, -1);
            swModel.ClearSelection2(true);

            nFaceCount = swFeature.GetAffectedFaceCount();
            Debug.Print("Number of faces affected by selected feature = " + nFaceCount);
            vAffectedFaces = (object[])swFeature.GetAffectedFaces();

            for (i = 0; i <= (nFaceCount - 1); i++)
            {
                nEdgeCount = ((Face)vAffectedFaces[i]).GetEdgeCount();
                Debug.Print("   Number of edges on Face " + i + " = " + nEdgeCount);
                vEdges = (object[])((Face)vAffectedFaces[i]).GetEdges();

                for (j = 0; j <= (nEdgeCount - 1); j++)
                {
                    ((Edge)vEdges[j]).Display(2, 0, 0, 1, true);
                }
            }
	

  • 遍历草图直线
//获取当前草图对象
                var swSketch = (Sketch)swModel.GetActiveSketch2();

                //获取该草图中的所有线
                var vSketchSeg = (object[])swSketch.GetSketchSegments();

                //定义选择
                SelectData swSelData = swSelMgr.CreateSelectData();

                SketchSegment swSketchSeg;
                //遍历线
                for (int i = 0; i < vSketchSeg.Length; i++)
                {
                    swSketchSeg = (SketchSegment)vSketchSeg[i];

                    swSketchSeg.Select4(false, swSelData);
                    
                }

      
  • 遍历草图中的点
			 var swSketch = (Sketch)swModel.GetActiveSketch2();
             object[] vSketchPt = (object[])swSketch.GetSketchPoints2();
                SketchPoint swSketchPt;
                //遍历点
                for (int i = 0; i < vSketchPt.Length; i++)
                {
                    swSketchPt = (SketchPoint)vSketchPt[i];        
                    
                }
  • 遍历装配中的零件
  swModel = (ModelDoc2)swApp.ActiveDoc;
  AssemblyDoc assemblyDoc = (AssemblyDoc)swModel 
 var allComps= (object[])assemblyDoc.GetComponents(false)

//第2种,遍历 装配体的特征
//第3种  TreeControlItem
 
  • 遍历视图

  var allViewsInThisSheet = (object[])sheet.GetViews();
  
  
  • 遍历视图中的尺寸
    Object[] displayDimensions = (Object[])swView.GetDisplayDimensions();
  • 遍历视图中的注释
var swNote = (Note)swView.GetFirstNote();
  while (swNote != null)
   {
       
        var swAnn = (Annotation)swNote.GetAnnotation();       
        swNote = (Note)swNote.GetNext();
    }
  • 遍历图纸
 var sheetNames = (string[])drawingDoc.GetSheetNames();
 for (int i = 0; i < sheetNames.Length; i++)
    {
      var bActiveSheet = drawingDoc.ActivateSheet(sheetNames[i]);
    }

  • 遍历视图中形位公差
var swGtol = (Gtol)swView.GetFirstGTOL();

 while (swGtol != null)
 {
     
     var swAnn = (Annotation)swGtol.GetAnnotation();
        swGtol = (Gtol)swGtol.GetNextGTOL();
 }
  • 遍历视图中的表面粗糙度
var swSfSymbol = (SFSymbol)swView.GetFirstSFSymbol();

  while (swSfSymbol != null)
  {
     
      var swAnn = (Annotation)swSfSymbol.GetAnnotation();
     
      swSfSymbol = (SFSymbol)swSfSymbol.GetNext();
  }
  • 遍历方程式
               //获取当前模型
                ModelDoc2 swModel = (ModelDoc2)swApp.ActiveDoc;
                //定义方程式管理器
                EquationMgr swEqnMgr = default(EquationMgr);

                int i = 0;
                int nCount = 0;

                if (swModel != null)
                {
                    swEqnMgr = (EquationMgr)swModel.GetEquationMgr();
                     nCount = swEqnMgr.GetCount();
                    for (i = 0; i < nCount; i++)
                    {
                        Debug.Print("  Equation(" + i + ")  = " + swEqnMgr.get_Equation(i));
                        Debug.Print("    Value = " + swEqnMgr.get_Value(i));
                        Debug.Print("    Index = " + swEqnMgr.Status);
                       Debug.Print("    Global variable? " + swEqnMgr.get_GlobalVariable(i));
                    }

                    //修改高度为60

                }
  • 遍历球标
//球标也是Note的一种,可以通过IsBomBalloon判断

            var noteCount = actionView.GetNoteCount();

            if (noteCount > 0)
            {
                Note note = (Note)actionView.GetFirstNote();           
                //note.IsBomBalloon() 是否是Bom球标
                Debug.Print(noteCount.ToString());
                Debug.Print(note.GetText());

                var notetext = note.GetText();
                     
                for (int k = 0; k < noteCount - 1; k++)
                {
                    note = (Note)note.GetNext();
               
                    notetext = note.GetText();
              
                }
            }


  • 遍历属性
               ModelDoc2 swModel = (ModelDoc2)swApp.ActiveDoc; //当前零件
                            
                var ConfigNames = (string[])swModel.GetConfigurationNames(); //所有配置名称

                Configuration swConfig = null;

                foreach (var configName in ConfigNames)//遍历所有配置
                {
                    swConfig = (Configuration)swModel.GetConfigurationByName(configName);
                    
                    var manger = swModel.Extension.CustomPropertyManager[configName];

                    object propNames = null;
                    object propTypes = null;
                    object propVaules = null;
                    object propResolveds = null;
                    object propLinks = null;

                    manger.GetAll3(ref propNames, ref propTypes, ref propVaules, ref propResolveds, ref propLinks);

                }
  • 遍历材料明细表
  public void ProcessTableAnn(SldWorks swApp, ModelDoc2 swModel, TableAnnotation swTableAnn, string ConfigName)
        {
            int nNumRow = 0;
            int J = 0;
            int I = 0;
            string ItemNumber = null;
            string PartNumber = null;
            bool RowLocked;
            double RowHeight;
 
            Debug.Print("   Table Title: " + swTableAnn.Title);
 
            nNumRow = swTableAnn.RowCount;
 
            BomTableAnnotation swBOMTableAnn = default(BomTableAnnotation);
            swBOMTableAnn = (BomTableAnnotation)swTableAnn;
 
            for (J = 0; J <= nNumRow - 1; J++)
            {
                RowLocked = swTableAnn.GetLockRowHeight(J);
                RowHeight = swTableAnn.GetRowHeight(J);
                Debug.Print("   Row Number " + J + " (height = " + RowHeight + "; height locked = " + RowLocked + ")");
                Debug.Print("     Component Count: " + swBOMTableAnn.GetComponentsCount2(J, ConfigName, out ItemNumber, out PartNumber));
                Debug.Print("       Item Number: " + ItemNumber);
                Debug.Print("       Part Number: " + PartNumber);
 
                object[] vPtArr = null;
                Component2 swComp = null;
                object pt = null;
 
                vPtArr = (object[])swBOMTableAnn.GetComponents2(J, ConfigName);
 
                if (((vPtArr != null)))
                {
                    for (I = 0; I <= vPtArr.GetUpperBound(0); I++)
                    {
                        pt = vPtArr[I];
                        swComp = (Component2)pt;
                        if ((swComp != null))
                        {
                            Debug.Print("           Component Name: " + swComp.Name2);
                            Debug.Print("           Configuration Name: " + swComp.ReferencedConfiguration);
                            Debug.Print("           Component Path: " + swComp.GetPathName());
                        }
                        else
                        {
                            Debug.Print("  Could not get component.");
                        }
                    }
                }
            }
        }
 
 
 
        public void ProcessBomFeature(SldWorks swApp, ModelDoc2 swModel, BomFeature swBomFeat)
        {
            Feature swFeat = default(Feature);
            object[] vTableArr = null;
            object vTable = null;
            string[] vConfigArray = null;
            object vConfig = null;
            string ConfigName = null;
            TableAnnotation swTable = default(TableAnnotation);
            object visibility = null;
 
            swFeat = swBomFeat.GetFeature();
            vTableArr = (object[])swBomFeat.GetTableAnnotations();
 
            foreach (TableAnnotation vTable_loopVariable in vTableArr)
            {
                vTable = vTable_loopVariable;
                swTable = (TableAnnotation)vTable;
                vConfigArray = (string[])swBomFeat.GetConfigurations(true, ref visibility);
                foreach (object vConfig_loopVariable in vConfigArray)
                {
                    vConfig = vConfig_loopVariable;
                    ConfigName = (string)vConfig;
                    Debug.Print(" Component for Configuration: " + ConfigName);
                    ProcessTableAnn(swApp, swModel, swTable, ConfigName);
                }
            }
 
        }
 
        public void Main()
        {
            ModelDoc2 swModel = default(ModelDoc2);
            DrawingDoc swDraw = default(DrawingDoc);
            Feature swFeat = default(Feature);
            BomFeature swBomFeat = default(BomFeature);
 
            swModel = (ModelDoc2)swApp.ActiveDoc;
            swDraw = (DrawingDoc)swModel;
            swFeat = (Feature)swModel.FirstFeature();
 
            while ((swFeat != null))
            {
                if ("BomFeat" == swFeat.GetTypeName())
                {
                    Debug.Print("Feature Name: " + swFeat.Name);
                    swBomFeat = (BomFeature)swFeat.GetSpecificFeature2();
                    ProcessBomFeature(swApp, swModel, swBomFeat);
                }
                swFeat = (Feature)swFeat.GetNextFeature();
            }
        }
 
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Paine Zeng

如果对有帮助,请我喝咖啡吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值