C# Solidworks二次开发:宏录制实战讲解(第二讲)

大家好,继续介绍我们的宏录制的例子讲解。

其实我们讲的是一个过程,大家只要理解这种方式就可以。

今天要介绍的第一例子为:

(1)如何创建一个轴线。

public void Main()

{

ModelDoc2 swDoc = null;

PartDoc swPart = null;

DrawingDoc swDrawing = null;

AssemblyDoc swAssembly = null;

bool boolstatus = false;

int longstatus = 0;

int longwarnings = 0;

swDoc = ((ModelDoc2)(swApp.ActiveDoc));

ModelView myModelView = null;

myModelView = ((ModelView)(swDoc.ActiveView));

myModelView.FrameState = ((int)(swWindowState_e.swWindowMaximized));

boolstatus = swDoc.Extension.SelectByRay(-0.012171036184511763, 0.035189282202352956, 0.0015284186583244264, -0.12907585697606233, 0.19912846068487891, -0.97143567944108677, 0.00015336652569445758, 2, false, 0, 0);

boolstatus = swDoc.InsertAxis2(true);

return;

}

这个例子也是比较简单,我们需要关注的API为InsertAxis2(true),下面是官方的解释:

然后下面是官方使用的例子:

This example shows how to create revolve features.

//----------------------------------------------------------------------------

// Preconditions:

// 1. Open a new part document.

// 2. Rename the namespace to match your C# project.

//

// Postconditions: Two revolve features and one cut-revolve feature are created.

//----------------------------------------------------------------------------

using SolidWorks.Interop.sldworks;

using SolidWorks.Interop.swconst;

using System;

namespace FeatureRevolves_CSharp.csproj

{

partial class SolidWorksMacro

{

ModelDoc2 swModel;

ModelDocExtension swModelDocExt;

FeatureManager swFeatMgr;

SelectionMgr swSelMgr;

bool boolstatus;

public void Main()

{

swModel = (ModelDoc2)swApp.ActiveDoc ;

swModelDocExt = swModel.Extension ;

swSelMgr = (SelectionMgr)swModel.SelectionManager ;

// Create an axis

boolstatus = swModelDocExt.SelectByID2 ("Right Plane", "PLANE", 0, 0, 0, true, 0, null, (int)swSelectOption_e.swSelectOptionDefault);

boolstatus = swModelDocExt.SelectByID2 ("Top Plane", "PLANE", 0, 0, 0, true, 0, null, (int)swSelectOption_e.swSelectOptionDefault);

swModel.InsertAxis2 (true);

// Create a rectangle

boolstatus = swModelDocExt.SelectByID2 ("Top Plane", "PLANE", -0.08954836342753, 0.0004336873289503, 0.006720765739942, false, 0, null, (int)swSelectOption_e.swSelectOptionDefault);

swModel.InsertSketch2 (true);

swModel.ClearSelection2 (true);

swModel.SketchRectangle (-0.05668466821757, -0.02198379306525, 0, -0.01330857427717, 0.03972855876814, 0, true);

// Create the first revolve feature

swModel.InsertSketch2 (true);

swModel.ShowNamedView2 ("*Trimetric", 8);

boolstatus = swModelDocExt.SelectByID2 ("Sketch1", "SKETCH", 0, 0, 0, false, 0, null, (int)swSelectOption_e.swSelectOptionDefault);

boolstatus = swModelDocExt.SelectByID2 ("Axis1", "AXIS", 0, 0, 0, true, 16, null, (int)swSelectOption_e.swSelectOptionDefault);

swFeatMgr = swModel.FeatureManager ;

swFeatMgr.FeatureRevolve2 (true, true, false, false, false, false, 0, 0, 6.28318530718, 0, false,

false, 0.01, 0.01, 0, 0, 0, true, true, true);

// Create a cut-revolve feature using a face on the revolve feature

swSelMgr.EnableContourSelection = false;

boolstatus = swModelDocExt.SelectByID2 ("", "FACE", -0.03095803920934, 0.01509536510872, 0.02198379306526, false, 0, null, (int)swSelectOption_e.swSelectOptionDefault);

swModel.InsertSketch2 (true);

swModel.ClearSelection2 (true);

swModel.SketchRectangle (-0.04194874421597, 0.01774859621099, 0, -0.01883036471929, -0.01265654504095, 0, true);

swModel.InsertSketch2 (true);

boolstatus = swModelDocExt.SelectByID2 ("Sketch2", "SKETCH", 0, 0, 0, false, 0, null, (int)swSelectOption_e.swSelectOptionDefault);

boolstatus = swModelDocExt.SelectByID2 ("Line4@Sketch2", "EXTSKETCHSEGMENT", -0.01883036471929, 0.003802500010693, 0, true, 4, null, (int)swSelectOption_e.swSelectOptionDefault);

swFeatMgr.FeatureRevolveCut (6.26573201466, false, 0, 0, 0, true, true);

// Create the second revolve feature using a face on the first revolve feature

swSelMgr.EnableContourSelection = false;

boolstatus = swModelDocExt.SelectByID2 ("", "FACE", -0.02333512246603, 0.03472018719853, 0.0219837930652, false, 0, null, (int)swSelectOption_e.swSelectOptionDefault);

swModel.InsertSketch2 (true);

swModel.ClearSelection2 (true);

swModel.CreateCircle2 (-0.02232361399104, 0.03354683337932, 0, -0.01445073476016, 0.02795861773112, 0);

swModel.InsertSketch2 (true);

boolstatus = swModelDocExt.SelectByID2 ("Sketch3", "SKETCH", 0, 0, 0, false, 0, null, (int)swSelectOption_e.swSelectOptionDefault);

boolstatus = swModelDocExt.SelectByRay (-1.81956067901865E-02, 1.80455411334037E-02, 2.17820530671702E-02, -0.400036026779312, -0.515038074910024, -0.758094294050284, 9.91972972972973E-04, 1, False, 4, 0);

swFeatMgr.FeatureRevolve2 (true, true, false, false, false, false, 0, 0, 6.28318530718, 0, false,

false, 0.01, 0.01, 0, 0, 0, true, true, true);

swSelMgr.EnableContourSelection = false;

}

public SldWorks swApp;

}

}

(2)第二个例子为显示选中组件。

public void Main()

{

ModelDoc2 swDoc = null;

PartDoc swPart = null;

DrawingDoc swDrawing = null;

AssemblyDoc swAssembly = null;

bool boolstatus = false;

int longstatus = 0;

int longwarnings = 0;

swDoc = ((ModelDoc2)(swApp.ActiveDoc));

boolstatus = swDoc.Extension.SelectByID2("gas box assembly.stp-1@52170019 GBX15WYS01 C.1 2023-08-13/box assembly.stp-1@gas " +

"box assembly.stp/door assembly.stp-1@box assembly.stp/R0502-0136_--6@door assemb" +

"ly.stp", "COMPONENT", 0, 0, 0, false, 0, null, 0);

swDoc.ShowComponent2();

return;

}

这个例子中需要关注的API是ShowComponent2(),下面是官方的解释:

今天要介绍的就是这么多,我们下篇文章再见。

相关推荐
小蜗牛慢慢爬行2 分钟前
有关异步场景的 10 大 Spring Boot 面试问题
java·开发语言·网络·spring boot·后端·spring·面试
Algorithm157612 分钟前
云原生相关的 Go 语言工程师技术路线(含博客网址导航)
开发语言·云原生·golang
shinelord明21 分钟前
【再谈设计模式】享元模式~对象共享的优化妙手
开发语言·数据结构·算法·设计模式·软件工程
呆呆小雅27 分钟前
C#关键字volatile
java·redis·c#
Monly2128 分钟前
Java(若依):修改Tomcat的版本
java·开发语言·tomcat
boligongzhu29 分钟前
DALSA工业相机SDK二次开发(图像采集及保存)C#版
开发语言·c#·dalsa
Eric.Lee202129 分钟前
moviepy将图片序列制作成视频并加载字幕 - python 实现
开发语言·python·音视频·moviepy·字幕视频合成·图像制作为视频
7yewh32 分钟前
嵌入式Linux QT+OpenCV基于人脸识别的考勤系统 项目
linux·开发语言·arm开发·驱动开发·qt·opencv·嵌入式linux
waicsdn_haha43 分钟前
Java/JDK下载、安装及环境配置超详细教程【Windows10、macOS和Linux图文详解】
java·运维·服务器·开发语言·windows·后端·jdk
_WndProc1 小时前
C++ 日志输出
开发语言·c++·算法