大家好,今天要讲的一些API是关于实体的相关API。
在开发的过程,很多地方会涉及到实体的相关操作,比如通过实体选中节点。下面就直接开始介绍API:
(1)第一个API为Select4,这个API的含义为选中一个实体,下面是API的官方解释:
输入参数有两个,第一个为ISelectData,第二个为布尔值。
返回值只有一个,成功选中会返回true,失败会返回false。
下面是官方使用的例子:
This example shows how to get data for an offset surface.
//----------------------------------------------------------------------
// Preconditions:
// 1. Open an assembly document that contains a component that
// has a surface offset feature.
// 2. Select the component's surface offset feature.
// 3. Open the Immediate window.
//
// Postconditions: Inspect the Immediate window.
//----------------------------------------------------------------------
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
namespace AnalyzeOffsetSurface_CSharp.csproj
{
partial class SolidWorksMacro
{
public void Main()
{
ModelDoc2 swModel = default(ModelDoc2);
SelectionMgr swSelMgr = default(SelectionMgr);
SelectData swSelData = default(SelectData);
SurfaceOffsetFeatureData swOffset = default(SurfaceOffsetFeatureData);
Feature swFeat = default(Feature);
Entity swEnt = default(Entity);
object[] vFace = null;
int i = 0;
bool bRet = false;
Component2 comp = default(Component2);
Component2 swCompFace = default(Component2);
swModel = (ModelDoc2)swApp.ActiveDoc;
swSelMgr = (SelectionMgr)swModel.SelectionManager;
swSelData = (SelectData)swSelMgr.CreateSelectData();
swFeat = (Feature)swSelMgr.GetSelectedObject6(1, -1);
swOffset = (SurfaceOffsetFeatureData)swFeat.GetDefinition();
comp = (Component2)swSelMgr.GetSelectedObjectsComponent3(1, -1);
Debug.Print("File = " + swModel.GetPathName());
Debug.Print("CompFeature = " + comp.Name2);
Debug.Print(" " + swFeat.Name);
Debug.Print(" Distance = " + swOffset.Distance * 1000.0 + " mm");
Debug.Print(" Flip = " + swOffset.Flip);
Debug.Print(" FacesCount = " + swOffset.GetEntitiesCount());
bRet = swOffset.AccessSelections(swModel, comp);
swModel.ClearSelection2(true);
vFace = (object[])swOffset.Entities;
for (i = 0; i <= vFace.GetUpperBound(0); i++)
{
swEnt = (Entity)vFace[i];
Debug.Print(" Entity selected = " + swEnt.Select4(true, null));
swCompFace = (Component2)swEnt.GetComponent();
Debug.Print(" Component face = " + swCompFace.Name2);
}
swOffset.ReleaseSelectionAccess();
}
public SldWorks swApp;
}
}
(2)第二个为GetType,这个API的含义为获取实体的类型,下面是API的具体解释:
方法没有输入值,返回值为这个实体的类型swSelectType_e。
下面是官方的例子:
This example shows how to get a component from an assembly feature.
//-----------------------------------------------------------------------------
// Preconditions:
// 1. Open an assembly document with at least one component.
// 2. Select a feature in a component in the FeatureManager design tree.
//
// Postconditions: Inspect the Immediate window.
//----------------------------------------------------------------------------
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
namespace GetComponentFromFeature_CSharp.csproj
{
partial class SolidWorksMacro
{
public void Main()
{
ModelDoc2 swModel = default(ModelDoc2);
Feature swFeature = default(Feature);
Entity swEntity = default(Entity);
bool bValue = false;
Component2 swComponent = default(Component2);
// Get active document
swModel = (ModelDoc2)swApp.ActiveDoc;
// Check the document is an assembly
if ((swModel.GetType() != (int)swDocumentTypes_e.swDocASSEMBLY))
{
return;
}
// Get the feature
swFeature = (Feature)((SelectionMgr)(swModel.SelectionManager)).GetSelectedObject6(1, -1);
// Cast the feature to an entity
swEntity = (Entity)swFeature;
// Get type through entity interface
Debug.Print("Entity type as defined in swSelectType_e: " + swEntity.GetType());
Debug.Assert(swEntity.GetType() == (int)swSelectType_e.swSelBODYFEATURES);
// Get type through feature interface
// Feature inherits from Entity, so will actually call Entity::GetType
Debug.Print("Entity type: " + swFeature.GetType());
// Get the component for the entity
swComponent = (Component2)swEntity.GetComponent();
// Print component details
Debug.Print(swComponent.Name2);
Debug.Print(" " + swComponent.GetPathName());
// Clear the selection lists
swModel.ClearSelection2(true);
// Select the feature through the Entity interface
bValue = swEntity.Select4(false, null);
// Print the type of the selected object
Debug.Print("Selected object type as defined in swSelectType_e: " + ((SelectionMgr)(swModel.SelectionManager)).GetSelectedObjectType3(1, -1));
Debug.Assert(((SelectionMgr)(swModel.SelectionManager)).GetSelectedObjectType3(1, -1) == (int)swSelectType_e.swSelBODYFEATURES);
// Clear the selection lists
swModel.ClearSelection2(true);
// Select the feature through the Feature interface
bValue = swFeature.Select2(false, 0);
// Print the type of the selected object
Debug.Print("Selected object type as defined in swSelectType_e: " + ((SelectionMgr)(swModel.SelectionManager)).GetSelectedObjectType3(1, -1));
Debug.Assert(((SelectionMgr)(swModel.SelectionManager)).GetSelectedObjectType3(1, -1) == (int)swSelectType_e.swSelBODYFEATURES);
}
public SldWorks swApp;
}
}
(3)第三个为FindAttribute,这个API的含义为查找实体上的属性,下面是API的具体解释:
参数的输入值有两个,第一个为要查找的属性,第二个为此实体上的类型实例。
今天要介绍的就是上面这三种API,本篇文章到此结束,我们下篇文章再见。