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,请检查!");
}
}
}
}
Add Attribute to Feature and Include in Library Feature Example (C#)
最新推荐文章于 2024-01-31 09:23:58 发布