图块实例

Solidworks的工程图中图块的使用也比较频繁。本文将给大家介绍Solidworks中与块有关的类SketchBlockDefinition及SketchBlockInstance。从字面上理解可以看出,与块定义有关的功能与SketchBlockDefinition相关。相同的块,可以被插入到工程图文档中的各张图中,则每张图中的块即为一个块的实例。

image.png

实例1:创建块

在打开的工程图中,任意绘制块所需的草图,注释,并将其全部选中,运行如下程序,即可自动将选中的元素做成块。

public static void CreateBlock(SldWorks SwApp, ModelDoc2 SwDoc, string BlockPath)
{
    SketchManager SwSktManager = SwDoc.SketchManager;
    MathUtility swMathUtility = SwApp.GetMathUtility();
    double[] xyz = new double[] { 0,0, 0 };//插入点
    MathPoint swMathPoint = swMathUtility.CreatePoint(xyz);
    SketchBlockDefinition SwBlockDefinition = SwSktManager.MakeSketchBlockFromSelected(swMathPoint);//将选中的元素做成块
    if (SwBlockDefinition != null)
    {
         SwBlockDefinition.Save(BlockPath);
    }
    else
    {
         MessageBox.Show("建块失败,请先选择需要做块的元素");
    }
}

运行效果:

image.png

实例2:插入图块

本例中将在工程图中插入同一个图块,可以切换不同的图纸,重复运行,实现插入多个图块实例。


public static void InsertBlock(SldWorks SwApp, ModelDoc2 SwDoc,string BlockPath)
{
    string BlockName = BlockPath.Substring(BlockPath.LastIndexOf(@"\")+1, BlockPath.LastIndexOf(".")- BlockPath.LastIndexOf(@"\") - 1);
    Feature SwFeat = ((DrawingDoc)SwDoc).FeatureByName(BlockName);

    SketchManager SwSktManager = SwDoc.SketchManager;
    MathUtility swMathUtility = SwApp.GetMathUtility();
    double[] xyz = new double[] { 1, 2, 3 };
    MathPoint swMathPoint = swMathUtility.CreatePoint(xyz);

    #region 全新插入
    if (SwFeat == null)
    {
         SketchBlockDefinition SwBlockDefinition = SwSktManager.MakeSketchBlockFromFile(swMathPoint, BlockPath, false, 1, 0);
    }
    #endregion
    #region 非全新插入--只要特征树又特征,就是非全新
    else
    {
        SketchBlockDefinition SwBlockDefinition = SwFeat.GetSpecificFeature2();
        SketchBlockInstance SwBlockIns = SwSktManager.InsertSketchBlockInstance(SwBlockDefinition, swMathPoint, 1, 0);
        SwBlockIns.Name = "ABC"+ (SwBlockDefinition.GetInstanceCount()+1).ToString().Trim();
        ((FeatureManager)SwDoc.FeatureManager).UpdateFeatureTree();
    }
    #endregion
    //一个块定义的所有实例是联动修改的
}

运行效果

每个图块都是块定义ABC的一个实例

image.png

实例3:获取工程图块信息

遍历工程图中图块的信息

public static void GetBlockIns(ModelDoc2 SwDoc)
{
    SketchManager SwSktManager = SwDoc.SketchManager;
    object[] ObjBlockDefs = SwSktManager.GetSketchBlockDefinitions();
    StringBuilder Sb = new StringBuilder("块定义数量:" + ObjBlockDefs.Length.ToString().Trim() + "\r\n");
    foreach (object ObjBlockDef in ObjBlockDefs)
    {
        SketchBlockDefinition SwBlockDef = (SketchBlockDefinition)ObjBlockDef;
        Sb.Append("块特征:"+SwBlockDef.GetFeature().Name+"\r\n");
        Sb.Append("引用路径:" + SwBlockDef.FileName+ "\r\n");
        Sb.Append("引用数量:" + SwBlockDef.GetInstanceCount().ToString().Trim() + "\r\n");
        if (SwBlockDef.GetInstanceCount() > 0)
        {
             Sb.Append("具体实例:\r\n");
             object[] ObjInses = SwBlockDef.GetInstances();
             foreach (object ObjIns in ObjInses)
             {
                  SketchBlockInstance SwBlockIns = (SketchBlockInstance)ObjIns;
                  Sb.Append("实例名:" + SwBlockIns.Name + "\r\n");
             }
         }
         Sb.Append("********************************\r\n");
    }
    System.Windows.Forms.MessageBox.Show(Sb.ToString().Trim());
}

运行效果:

image.png

你可能感兴趣的:(图块实例)