Add Attribute to Feature and Include in Library Feature Example (C#)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Add_Attribute_to_Feature
{
    class Program
    {

        public static SldWorks swApp;

        static void Main(string[] args)
        {
            ConnectSw();
            ModelDoc2 swModel = default(ModelDoc2);
            ModelDocExtension swModelDocExt = default(ModelDocExtension);
            SelectionMgr swSelectionMgr = default(SelectionMgr);
            Feature swFeature = default(Feature);
            SolidWorks.Interop.sldworks.Attribute swAttribute = default(SolidWorks.Interop.sldworks.Attribute);
            AttributeDef swAttributeDef = default(AttributeDef);
            Face2 swFace = default(Face2);
            Parameter swParameter = default(Parameter);
            Object[] Faces = null;
            bool @bool = false;

            swModel = (ModelDoc2)swApp.ActiveDoc;
            swModelDocExt = swModel.Extension;
            swSelectionMgr = (SelectionMgr)swModel.SelectionManager;

            // Create attribute 
            swAttributeDef = (AttributeDef)swApp.DefineAttribute("TestPropagationOfAttribute");
            @bool = swAttributeDef.AddParameter("TestAttribute", (int)swParamType_e.swParamTypeDouble, 2.0, 0);
            @bool = swAttributeDef.Register();

            // Select the feature to which to add the attribute 
            @bool = swModelDocExt.SelectByID2("Cut-Extrude1", "BODYFEATURE", 0, 0, 0, false, 0, null, 0);
            swFeature = (Feature)swSelectionMgr.GetSelectedObject6(1, -1);
            Debug.Print("Name of feature to which to add attribute: " + swFeature.Name);

            // Add the attribute to one of the feature's faces 
            Faces = (Object[])swFeature.GetFaces();
            swFace = (Face2)Faces[0];
            swAttribute = swAttributeDef.CreateInstance5(swModel, swFace, "TestAttribute", 0, (int)swInConfigurationOpts_e.swAllConfiguration);
            swAttribute.IncludeInLibraryFeature = true;
            Debug.Print("Include attribute in library feature? " + swAttribute.IncludeInLibraryFeature);
            Debug.Print("Name of attribute: " + swAttribute.GetName());
            // Get name of parameter
            swParameter = (Parameter)swAttribute.GetParameter("TestAttribute");
            Debug.Print("Parameter name: " + swParameter.GetName());

            swModel.ForceRebuild3(false);


        }
        public static void ConnectSw()

        {

            //尝试连接已打开的Solidworks

            if (swApp == null)

            {

                Process[] processArr = null;

                processArr = Process.GetProcessesByName("SLDWORKS");

                if (processArr.Length > 0)

                {

                    //通过SldWorks.Application方式

                    try

                    {

                        swApp = (SldWorks)Marshal.GetActiveObject("SldWorks.Application");

                    }

                    catch (Exception)

                    {



                    }

                    //通过SldWorks.Application.18方式,.18=2010版,.19=2011版,.20=2012版,.21=2013版,.22=2014版,.23=2015版

                    if (swApp == null)

                    {

                        for (int i = 18; i < 28; i++)

                        {

                            try

                            {

                                swApp = (SldWorks)Marshal.GetActiveObject("SldWorks.Application." + i.ToString());

                                if (swApp != null)

                                {

                                    break;

                                }

                            }

                            catch (Exception)

                            {



                            }

                        }

                    }

                    //通过Guid的方式

                    if (swApp == null)//2010sw

                    {

                        try

                        {

                            Guid myGuid1 = new Guid("6894540C-3171-484F-9E97-6A962559BA30");

                            object processSW = System.Activator.CreateInstance(System.Type.GetTypeFromCLSID(myGuid1));

                            swApp = (SldWorks)processSW;

                        }

                        catch (Exception)

                        {



                        }

                    }

                    if (swApp == null)//2012sw

                    {

                        try

                        {

                            Guid myGuid1 = new Guid("B4875E89-91F6-4124-BB63-2539727E98FA");

                            object processSW = System.Activator.CreateInstance(System.Type.GetTypeFromCLSID(myGuid1));

                            swApp = (SldWorks)processSW;

                        }

                        catch (Exception)

                        {



                        }

                    }

                    //通过SldWorks.ISldWorks方式

                    if (swApp == null)

                    {

                        try

                        {

                            swApp = (SldWorks)Marshal.GetActiveObject("SldWorks.ISldWorks");

                        }

                        catch (Exception)

                        {



                        }

                    }

                }

            }

            //尝试连接注册表中注册的solidworks,可未打开状态,SldWorksClass() 方式

            if (swApp == null)

            {

                //try

                //{

                //  swApp = new SldWorksClass();

                //}

                //catch (Exception)

                //{



                //}

            }

            //SldWorks()方式连接

            if (swApp == null)

            {

                try

                {

                    swApp = new SldWorks();

                }

                catch (Exception)

                {



                }

            }

            //连接成功,使solidworks可见

            if (swApp != null)

            {

                if (!swApp.Visible)

                {

                    try

                    {

                        swApp.Visible = true;

                    }

                    catch

                    {

                        swApp = null;

                    }

                }

            }

            //连接失败

            if (swApp == null)

            {

                throw new Exception("无法连接到Solidworks,请检查!");

            }

        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值