ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2020-06-30 14:06:14  阅读:16  来源: 互联网

标签:遍历 C# SolidWorks swEqnMgr var 二次开发 swModel null Debug


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

  • 遍历图块
       ///这是根据名字来遍历图块的定义
       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];        
                    
                }
  • 遍历装配中的零件
请参考之前的博文
  • 遍历视图

  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

                }
  • 遍历球标
  • 遍历属性

标签:遍历,C#,SolidWorks,swEqnMgr,var,二次开发,swModel,null,Debug
来源: https://blog.csdn.net/zengqh0314/article/details/105922970

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有