├── .gitignore ├── LICENSE ├── Readme.md ├── addinsrc └── SwTools │ ├── SwTools.Views │ ├── FeatMgrTab.xaml │ ├── FeatMgrTab.xaml.cs │ ├── Icons │ │ └── library.png │ ├── ModelViewTab.xaml │ ├── ModelViewTab.xaml.cs │ ├── Properties │ │ ├── AssemblyInfo.cs │ │ ├── Resources.Designer.cs │ │ ├── Resources.resx │ │ ├── Settings.Designer.cs │ │ └── Settings.settings │ ├── SwTools.Views.csproj │ ├── Taskpane.xaml │ ├── Taskpane.xaml.cs │ ├── TestWindow.xaml │ ├── TestWindow.xaml.cs │ ├── Themes │ │ ├── Generic.xaml │ │ └── taskpane_resourcedictionary.xaml │ └── packages.config │ ├── SwTools.sln │ └── SwTools │ ├── Addin.cs │ ├── Icons │ └── edit.png │ ├── Properties │ ├── AssemblyInfo.cs │ ├── Resource.Designer.cs │ └── Resource.resx │ ├── SwCommands.cs │ ├── SwTools.csproj │ └── packages.config ├── resources └── chapter1-hellosoliworks.png └── src ├── Chapter1.ConnectToSw ├── App.config ├── Chapter1.ConnectToSw.csproj ├── Program.cs ├── Properties │ └── AssemblyInfo.cs ├── Readme.md └── packages.config ├── Chapter2.NewDocument ├── App.config ├── App.xaml ├── App.xaml.cs ├── Chapter2.NewDocument.csproj ├── MainViewModel.cs ├── MainWindow.xaml ├── MainWindow.xaml.cs ├── Model │ ├── 工程图1.SLDDRW │ ├── 装配体2.SLDASM │ └── 零件1.SLDPRT ├── Properties │ ├── AssemblyInfo.cs │ ├── Resources.Designer.cs │ ├── Resources.resx │ ├── Settings.Designer.cs │ └── Settings.settings ├── Readme.md └── packages.config ├── Chapter3.PartAutomation ├── App.config ├── App.xaml ├── App.xaml.cs ├── Chapter3.PartAutomation.csproj ├── Extension │ ├── IModelDocExtension.cs │ └── ISldWorksExtension.cs ├── MainWindow.xaml ├── MainWindow.xaml.cs ├── Properties │ ├── AssemblyInfo.cs │ ├── Resources.Designer.cs │ ├── Resources.resx │ ├── Settings.Designer.cs │ └── Settings.settings ├── packages.config └── readme.md ├── Chapter4.MyFirstAddin ├── Chapter4.MyFirstAddin.csproj ├── MyFirstAddin.cs ├── Properties │ ├── AssemblyInfo.cs │ ├── Resource.Designer.cs │ └── Resource.resx ├── RegAsm.exe ├── Resource │ └── MyFirstCommand.png └── packages.config └── SolidWorksTutorial.sln /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Aa][Rr][Mm]/ 27 | [Aa][Rr][Mm]64/ 28 | bld/ 29 | [Bb]in/ 30 | [Oo]bj/ 31 | [Ll]og/ 32 | [Ll]ogs/ 33 | 34 | # Visual Studio 2015/2017 cache/options directory 35 | .vs/ 36 | # Uncomment if you have tasks that create the project's static files in wwwroot 37 | #wwwroot/ 38 | 39 | # Visual Studio 2017 auto generated files 40 | Generated\ Files/ 41 | 42 | # MSTest test Results 43 | [Tt]est[Rr]esult*/ 44 | [Bb]uild[Ll]og.* 45 | 46 | # NUnit 47 | *.VisualState.xml 48 | TestResult.xml 49 | nunit-*.xml 50 | 51 | # Build Results of an ATL Project 52 | [Dd]ebugPS/ 53 | [Rr]eleasePS/ 54 | dlldata.c 55 | 56 | # Benchmark Results 57 | BenchmarkDotNet.Artifacts/ 58 | 59 | # .NET Core 60 | project.lock.json 61 | project.fragment.lock.json 62 | artifacts/ 63 | 64 | # StyleCop 65 | StyleCopReport.xml 66 | 67 | # Files built by Visual Studio 68 | *_i.c 69 | *_p.c 70 | *_h.h 71 | *.ilk 72 | *.meta 73 | *.obj 74 | *.iobj 75 | *.pch 76 | *.pdb 77 | *.ipdb 78 | *.pgc 79 | *.pgd 80 | *.rsp 81 | *.sbr 82 | *.tlb 83 | *.tli 84 | *.tlh 85 | *.tmp 86 | *.tmp_proj 87 | *_wpftmp.csproj 88 | *.log 89 | *.vspscc 90 | *.vssscc 91 | .builds 92 | *.pidb 93 | *.svclog 94 | *.scc 95 | 96 | # Chutzpah Test files 97 | _Chutzpah* 98 | 99 | # Visual C++ cache files 100 | ipch/ 101 | *.aps 102 | *.ncb 103 | *.opendb 104 | *.opensdf 105 | *.sdf 106 | *.cachefile 107 | *.VC.db 108 | *.VC.VC.opendb 109 | 110 | # Visual Studio profiler 111 | *.psess 112 | *.vsp 113 | *.vspx 114 | *.sap 115 | 116 | # Visual Studio Trace Files 117 | *.e2e 118 | 119 | # TFS 2012 Local Workspace 120 | $tf/ 121 | 122 | # Guidance Automation Toolkit 123 | *.gpState 124 | 125 | # ReSharper is a .NET coding add-in 126 | _ReSharper*/ 127 | *.[Rr]e[Ss]harper 128 | *.DotSettings.user 129 | 130 | # TeamCity is a build add-in 131 | _TeamCity* 132 | 133 | # DotCover is a Code Coverage Tool 134 | *.dotCover 135 | 136 | # AxoCover is a Code Coverage Tool 137 | .axoCover/* 138 | !.axoCover/settings.json 139 | 140 | # Visual Studio code coverage results 141 | *.coverage 142 | *.coveragexml 143 | 144 | # NCrunch 145 | _NCrunch_* 146 | .*crunch*.local.xml 147 | nCrunchTemp_* 148 | 149 | # MightyMoose 150 | *.mm.* 151 | AutoTest.Net/ 152 | 153 | # Web workbench (sass) 154 | .sass-cache/ 155 | 156 | # Installshield output folder 157 | [Ee]xpress/ 158 | 159 | # DocProject is a documentation generator add-in 160 | DocProject/buildhelp/ 161 | DocProject/Help/*.HxT 162 | DocProject/Help/*.HxC 163 | DocProject/Help/*.hhc 164 | DocProject/Help/*.hhk 165 | DocProject/Help/*.hhp 166 | DocProject/Help/Html2 167 | DocProject/Help/html 168 | 169 | # Click-Once directory 170 | publish/ 171 | 172 | # Publish Web Output 173 | *.[Pp]ublish.xml 174 | *.azurePubxml 175 | # Note: Comment the next line if you want to checkin your web deploy settings, 176 | # but database connection strings (with potential passwords) will be unencrypted 177 | *.pubxml 178 | *.publishproj 179 | 180 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 181 | # checkin your Azure Web App publish settings, but sensitive information contained 182 | # in these scripts will be unencrypted 183 | PublishScripts/ 184 | 185 | # NuGet Packages 186 | *.nupkg 187 | # NuGet Symbol Packages 188 | *.snupkg 189 | # The packages folder can be ignored because of Package Restore 190 | **/[Pp]ackages/* 191 | # except build/, which is used as an MSBuild target. 192 | !**/[Pp]ackages/build/ 193 | # Uncomment if necessary however generally it will be regenerated when needed 194 | #!**/[Pp]ackages/repositories.config 195 | # NuGet v3's project.json files produces more ignorable files 196 | *.nuget.props 197 | *.nuget.targets 198 | 199 | # Microsoft Azure Build Output 200 | csx/ 201 | *.build.csdef 202 | 203 | # Microsoft Azure Emulator 204 | ecf/ 205 | rcf/ 206 | 207 | # Windows Store app package directories and files 208 | AppPackages/ 209 | BundleArtifacts/ 210 | Package.StoreAssociation.xml 211 | _pkginfo.txt 212 | *.appx 213 | *.appxbundle 214 | *.appxupload 215 | 216 | # Visual Studio cache files 217 | # files ending in .cache can be ignored 218 | *.[Cc]ache 219 | # but keep track of directories ending in .cache 220 | !?*.[Cc]ache/ 221 | 222 | # Others 223 | ClientBin/ 224 | ~$* 225 | *~ 226 | *.dbmdl 227 | *.dbproj.schemaview 228 | *.jfm 229 | *.pfx 230 | *.publishsettings 231 | orleans.codegen.cs 232 | 233 | # Including strong name files can present a security risk 234 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 235 | #*.snk 236 | 237 | # Since there are multiple workflows, uncomment next line to ignore bower_components 238 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 239 | #bower_components/ 240 | 241 | # RIA/Silverlight projects 242 | Generated_Code/ 243 | 244 | # Backup & report files from converting an old project file 245 | # to a newer Visual Studio version. Backup files are not needed, 246 | # because we have git ;-) 247 | _UpgradeReport_Files/ 248 | Backup*/ 249 | UpgradeLog*.XML 250 | UpgradeLog*.htm 251 | ServiceFabricBackup/ 252 | *.rptproj.bak 253 | 254 | # SQL Server files 255 | *.mdf 256 | *.ldf 257 | *.ndf 258 | 259 | # Business Intelligence projects 260 | *.rdl.data 261 | *.bim.layout 262 | *.bim_*.settings 263 | *.rptproj.rsuser 264 | *- [Bb]ackup.rdl 265 | *- [Bb]ackup ([0-9]).rdl 266 | *- [Bb]ackup ([0-9][0-9]).rdl 267 | 268 | # Microsoft Fakes 269 | FakesAssemblies/ 270 | 271 | # GhostDoc plugin setting file 272 | *.GhostDoc.xml 273 | 274 | # Node.js Tools for Visual Studio 275 | .ntvs_analysis.dat 276 | node_modules/ 277 | 278 | # Visual Studio 6 build log 279 | *.plg 280 | 281 | # Visual Studio 6 workspace options file 282 | *.opt 283 | 284 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 285 | *.vbw 286 | 287 | # Visual Studio LightSwitch build output 288 | **/*.HTMLClient/GeneratedArtifacts 289 | **/*.DesktopClient/GeneratedArtifacts 290 | **/*.DesktopClient/ModelManifest.xml 291 | **/*.Server/GeneratedArtifacts 292 | **/*.Server/ModelManifest.xml 293 | _Pvt_Extensions 294 | 295 | # Paket dependency manager 296 | .paket/paket.exe 297 | paket-files/ 298 | 299 | # FAKE - F# Make 300 | .fake/ 301 | 302 | # CodeRush personal settings 303 | .cr/personal 304 | 305 | # Python Tools for Visual Studio (PTVS) 306 | __pycache__/ 307 | *.pyc 308 | 309 | # Cake - Uncomment if you are using it 310 | # tools/** 311 | # !tools/packages.config 312 | 313 | # Tabs Studio 314 | *.tss 315 | 316 | # Telerik's JustMock configuration file 317 | *.jmconfig 318 | 319 | # BizTalk build output 320 | *.btp.cs 321 | *.btm.cs 322 | *.odx.cs 323 | *.xsd.cs 324 | 325 | # OpenCover UI analysis results 326 | OpenCover/ 327 | 328 | # Azure Stream Analytics local run output 329 | ASALocalRun/ 330 | 331 | # MSBuild Binary and Structured Log 332 | *.binlog 333 | 334 | # NVidia Nsight GPU debugger configuration file 335 | *.nvuser 336 | 337 | # MFractors (Xamarin productivity tool) working folder 338 | .mfractor/ 339 | 340 | # Local History for Visual Studio 341 | .localhistory/ 342 | 343 | # BeatPulse healthcheck temp database 344 | healthchecksdb 345 | 346 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 347 | MigrationBackup/ 348 | 349 | # Ionide (cross platform F# VS Code tools) working folder 350 | .ionide/ 351 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 dududu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weianweigan/SolidWorksAPITutorial/147432e8bf6ed1499849de78448ad307264179f6/Readme.md -------------------------------------------------------------------------------- /addinsrc/SwTools/SwTools.Views/FeatMgrTab.xaml: -------------------------------------------------------------------------------- 1 |  10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /addinsrc/SwTools/SwTools.Views/FeatMgrTab.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Windows; 7 | using System.Windows.Controls; 8 | using System.Windows.Data; 9 | using System.Windows.Documents; 10 | using System.Windows.Input; 11 | using System.Windows.Media; 12 | using System.Windows.Media.Imaging; 13 | using System.Windows.Navigation; 14 | using System.Windows.Shapes; 15 | using SolidWorks.Interop.sldworks; 16 | 17 | namespace SwTools.Views 18 | { 19 | /// 20 | /// FeatMgrTab.xaml 的交互逻辑 21 | /// 22 | public partial class FeatMgrTab : UserControl 23 | { 24 | public FeatMgrTab() 25 | { 26 | InitializeComponent(); 27 | } 28 | 29 | public ISldWorks Sw { get; set; } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /addinsrc/SwTools/SwTools.Views/Icons/library.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weianweigan/SolidWorksAPITutorial/147432e8bf6ed1499849de78448ad307264179f6/addinsrc/SwTools/SwTools.Views/Icons/library.png -------------------------------------------------------------------------------- /addinsrc/SwTools/SwTools.Views/ModelViewTab.xaml: -------------------------------------------------------------------------------- 1 |  10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 19 | 20 | 21 | 22 | 23 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /addinsrc/SwTools/SwTools.Views/ModelViewTab.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Windows; 7 | using System.Windows.Controls; 8 | using System.Windows.Data; 9 | using System.Windows.Documents; 10 | using System.Windows.Input; 11 | using System.Windows.Media; 12 | using System.Windows.Media.Imaging; 13 | using System.Windows.Navigation; 14 | using System.Windows.Shapes; 15 | using SolidWorks.Interop.sldworks; 16 | 17 | namespace SwTools.Views 18 | { 19 | /// 20 | /// ModelViewTab.xaml 的交互逻辑 21 | /// 22 | public partial class ModelViewTab : UserControl 23 | { 24 | public ModelViewTab() 25 | { 26 | InitializeComponent(); 27 | } 28 | 29 | public ISldWorks Sw { get; set; } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /addinsrc/SwTools/SwTools.Views/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Resources; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.InteropServices; 5 | using System.Windows; 6 | 7 | // 有关程序集的一般信息由以下 8 | // 控制。更改这些特性值可修改 9 | // 与程序集关联的信息。 10 | [assembly: AssemblyTitle("SwTools.Views")] 11 | [assembly: AssemblyDescription("")] 12 | [assembly: AssemblyConfiguration("")] 13 | [assembly: AssemblyCompany("")] 14 | [assembly: AssemblyProduct("SwTools.Views")] 15 | [assembly: AssemblyCopyright("Copyright © 2022")] 16 | [assembly: AssemblyTrademark("")] 17 | [assembly: AssemblyCulture("")] 18 | 19 | //将 ComVisible 设置为 false 将使此程序集中的类型 20 | //对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型, 21 | //请将此类型的 ComVisible 特性设置为 true。 22 | [assembly: ComVisible(false)] 23 | 24 | //若要开始生成可本地化的应用程序,请设置 25 | //.csproj 文件中的 CultureYouAreCodingWith 26 | //例如,如果您在源文件中使用的是美国英语, 27 | //使用的是美国英语,请将 设置为 en-US。 然后取消 28 | //对以下 NeutralResourceLanguage 特性的注释。 更新 29 | //以下行中的“en-US”以匹配项目文件中的 UICulture 设置。 30 | 31 | //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] 32 | 33 | 34 | [assembly:ThemeInfo( 35 | ResourceDictionaryLocation.None, //主题特定资源词典所处位置 36 | //(未在页面中找到资源时使用, 37 | //或应用程序资源字典中找到时使用) 38 | ResourceDictionaryLocation.SourceAssembly //常规资源词典所处位置 39 | //(未在页面中找到资源时使用, 40 | //、应用程序或任何主题专用资源字典中找到时使用) 41 | )] 42 | 43 | 44 | // 程序集的版本信息由下列四个值组成: 45 | // 46 | // 主版本 47 | // 次版本 48 | // 生成号 49 | // 修订号 50 | // 51 | //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 52 | //通过使用 "*",如下所示: 53 | // [assembly: AssemblyVersion("1.0.*")] 54 | [assembly: AssemblyVersion("1.0.0.0")] 55 | [assembly: AssemblyFileVersion("1.0.0.0")] 56 | -------------------------------------------------------------------------------- /addinsrc/SwTools/SwTools.Views/Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // 此代码由工具生成。 4 | // 运行时版本:4.0.30319.42000 5 | // 6 | // 对此文件的更改可能会导致不正确的行为,并且如果 7 | // 重新生成代码,这些更改将会丢失。 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace SwTools.Views.Properties { 12 | using System; 13 | 14 | 15 | /// 16 | /// 一个强类型的资源类,用于查找本地化的字符串等。 17 | /// 18 | // 此类是由 StronglyTypedResourceBuilder 19 | // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 20 | // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen 21 | // (以 /str 作为命令选项),或重新生成 VS 项目。 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class Resources { 26 | 27 | private static global::System.Resources.ResourceManager resourceMan; 28 | 29 | private static global::System.Globalization.CultureInfo resourceCulture; 30 | 31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 32 | internal Resources() { 33 | } 34 | 35 | /// 36 | /// 返回此类使用的缓存的 ResourceManager 实例。 37 | /// 38 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 39 | internal static global::System.Resources.ResourceManager ResourceManager { 40 | get { 41 | if (object.ReferenceEquals(resourceMan, null)) { 42 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("SwTools.Views.Properties.Resources", typeof(Resources).Assembly); 43 | resourceMan = temp; 44 | } 45 | return resourceMan; 46 | } 47 | } 48 | 49 | /// 50 | /// 重写当前线程的 CurrentUICulture 属性,对 51 | /// 使用此强类型资源类的所有资源查找执行重写。 52 | /// 53 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 54 | internal static global::System.Globalization.CultureInfo Culture { 55 | get { 56 | return resourceCulture; 57 | } 58 | set { 59 | resourceCulture = value; 60 | } 61 | } 62 | 63 | /// 64 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 65 | /// 66 | internal static System.Drawing.Bitmap library { 67 | get { 68 | object obj = ResourceManager.GetObject("library", resourceCulture); 69 | return ((System.Drawing.Bitmap)(obj)); 70 | } 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /addinsrc/SwTools/SwTools.Views/Properties/Resources.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | 121 | 122 | ..\icons\library.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 123 | 124 | -------------------------------------------------------------------------------- /addinsrc/SwTools/SwTools.Views/Properties/Settings.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.42000 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace SwTools.Views.Properties 12 | { 13 | 14 | 15 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 16 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] 17 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase 18 | { 19 | 20 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); 21 | 22 | public static Settings Default 23 | { 24 | get 25 | { 26 | return defaultInstance; 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /addinsrc/SwTools/SwTools.Views/Properties/Settings.settings: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /addinsrc/SwTools/SwTools.Views/SwTools.Views.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | net472 4 | Library 5 | false 6 | true 7 | true 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /addinsrc/SwTools/SwTools.Views/Taskpane.xaml: -------------------------------------------------------------------------------- 1 |  11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 24 | 25 | 26 | 29 | 31 | 32 | 33 | 34 | 35 | 36 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /addinsrc/SwTools/SwTools.Views/Taskpane.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Windows; 7 | using System.Windows.Controls; 8 | using System.Windows.Data; 9 | using System.Windows.Documents; 10 | using System.Windows.Input; 11 | using System.Windows.Media; 12 | using System.Windows.Media.Imaging; 13 | using System.Windows.Navigation; 14 | using System.Windows.Shapes; 15 | using SolidWorks.Interop.sldworks; 16 | using SwTools.Views.Properties; 17 | using Xarial.XCad.Base.Attributes; 18 | 19 | namespace SwTools.Views 20 | { 21 | /// 22 | /// Taskpane.xaml 的交互逻辑 23 | /// 24 | [Title("SwToolsLibrary")] 25 | [Icon(typeof(Resources), nameof(Properties.Resources.library))] 26 | public partial class Taskpane : UserControl 27 | { 28 | public Taskpane() 29 | { 30 | InitializeComponent(); 31 | } 32 | 33 | public ISldWorks Sw { get; set; } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /addinsrc/SwTools/SwTools.Views/TestWindow.xaml: -------------------------------------------------------------------------------- 1 |  12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /src/Chapter2.NewDocument/MainWindow.xaml.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | using System.IO; 3 | using System.Linq; 4 | using System.Windows; 5 | using SolidWorks.Interop.swconst; 6 | using Xarial.XCad.SolidWorks; 7 | 8 | namespace Chapter2.NewDocument 9 | { 10 | /// 11 | /// MainWindow.xaml 的交互逻辑 12 | /// 13 | public partial class MainWindow : Window 14 | { 15 | private ISwApplication _swApp; 16 | 17 | public MainWindow() 18 | { 19 | InitializeComponent(); 20 | DataContext = new MainViewModel(); 21 | } 22 | 23 | #region 新建SolidWorks文档 24 | 25 | /// 26 | /// 新建零件 27 | /// 28 | private void Button_Click_1(object sender, RoutedEventArgs e) 29 | { 30 | NewSolidWorksDoc(swUserPreferenceStringValue_e.swDefaultTemplatePart); 31 | } 32 | 33 | /// 34 | /// 新建装配体 35 | /// 36 | private void Button_Click_2(object sender, RoutedEventArgs e) 37 | { 38 | NewSolidWorksDoc(swUserPreferenceStringValue_e.swDefaultTemplateAssembly); 39 | } 40 | 41 | /// 42 | /// 新建工程图 43 | /// 44 | private void Button_Click_3(object sender, RoutedEventArgs e) 45 | { 46 | NewSolidWorksDoc(swUserPreferenceStringValue_e.swDefaultTemplateDrawing); 47 | } 48 | 49 | /// 50 | /// 根据SolidWorks默认配置的模板路径,新建SolidWorks文档 51 | /// 52 | /// 默认模板类型 53 | private void NewSolidWorksDoc(swUserPreferenceStringValue_e docTemplateType) 54 | { 55 | if (_swApp == null) 56 | { 57 | MessageBox.Show("未连接SolidWorks"); 58 | return; 59 | } 60 | 61 | var templatePart = _swApp.Sw.GetUserPreferenceStringValue((int)docTemplateType); 62 | 63 | if (!File.Exists(templatePart)) 64 | { 65 | MessageBox.Show("未支配默认模板,无法新建零件"); 66 | return; 67 | } 68 | 69 | var doc = _swApp.Sw.INewDocument2(templatePart, 0, 300d, 300d); 70 | } 71 | 72 | #endregion 73 | 74 | #region 打开文档 75 | 76 | /// 77 | /// 打开当前存在的零件 78 | /// 79 | private void Button_Click_4(object sender, RoutedEventArgs e) 80 | { 81 | OpenLocalDoc("零件1.sldprt", swDocumentTypes_e.swDocPART); 82 | } 83 | 84 | /// 85 | /// 打开本地文档 86 | /// 87 | private void OpenLocalDoc(string docName, swDocumentTypes_e docType) 88 | { 89 | string partDocPath = Path.Combine( 90 | Path.GetDirectoryName(typeof(MainWindow).Assembly.Location), 91 | "Model", 92 | docName); 93 | 94 | if (_swApp == null) 95 | { 96 | MessageBox.Show("未连接SolidWorks"); 97 | return; 98 | } 99 | 100 | if (!File.Exists(partDocPath)) 101 | { 102 | _swApp.ShowMessageBox($"{partDocPath} 此文件不存在"); 103 | return; 104 | } 105 | 106 | int errors = 0; 107 | int warings = 0; 108 | 109 | var partDoc = _swApp.Sw.OpenDoc6(partDocPath, (int)docType, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warings); 110 | 111 | if (partDoc == null) 112 | { 113 | _swApp.ShowMessageBox($"{partDocPath} 打开失败,错误代码:{errors}"); 114 | } 115 | } 116 | 117 | /// 118 | /// 打开装配体文档 119 | /// 120 | private void Button_Click_5(object sender, RoutedEventArgs e) 121 | { 122 | OpenLocalDoc("装配体2.sldasm", swDocumentTypes_e.swDocASSEMBLY); 123 | } 124 | 125 | /// 126 | /// 打开工程图文档 127 | /// 128 | private void Button_Click_6(object sender, RoutedEventArgs e) 129 | { 130 | OpenLocalDoc("工程图1.slddrw", swDocumentTypes_e.swDocDRAWING); 131 | } 132 | 133 | #endregion 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /src/Chapter2.NewDocument/Model/工程图1.SLDDRW: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weianweigan/SolidWorksAPITutorial/147432e8bf6ed1499849de78448ad307264179f6/src/Chapter2.NewDocument/Model/工程图1.SLDDRW -------------------------------------------------------------------------------- /src/Chapter2.NewDocument/Model/装配体2.SLDASM: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weianweigan/SolidWorksAPITutorial/147432e8bf6ed1499849de78448ad307264179f6/src/Chapter2.NewDocument/Model/装配体2.SLDASM -------------------------------------------------------------------------------- /src/Chapter2.NewDocument/Model/零件1.SLDPRT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weianweigan/SolidWorksAPITutorial/147432e8bf6ed1499849de78448ad307264179f6/src/Chapter2.NewDocument/Model/零件1.SLDPRT -------------------------------------------------------------------------------- /src/Chapter2.NewDocument/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Resources; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.InteropServices; 5 | using System.Windows; 6 | 7 | // 有关程序集的一般信息由以下 8 | // 控制。更改这些特性值可修改 9 | // 与程序集关联的信息。 10 | [assembly: AssemblyTitle("Chapter2.NewDocument")] 11 | [assembly: AssemblyDescription("")] 12 | [assembly: AssemblyConfiguration("")] 13 | [assembly: AssemblyCompany("")] 14 | [assembly: AssemblyProduct("Chapter2.NewDocument")] 15 | [assembly: AssemblyCopyright("Copyright © 2021")] 16 | [assembly: AssemblyTrademark("")] 17 | [assembly: AssemblyCulture("")] 18 | 19 | // 将 ComVisible 设置为 false 会使此程序集中的类型 20 | //对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 21 | //请将此类型的 ComVisible 特性设置为 true。 22 | [assembly: ComVisible(false)] 23 | 24 | //若要开始生成可本地化的应用程序,请设置 25 | //.csproj 文件中的 CultureYouAreCodingWith 26 | //例如,如果您在源文件中使用的是美国英语, 27 | //使用的是美国英语,请将 设置为 en-US。 然后取消 28 | //对以下 NeutralResourceLanguage 特性的注释。 更新 29 | //以下行中的“en-US”以匹配项目文件中的 UICulture 设置。 30 | 31 | //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] 32 | 33 | 34 | [assembly: ThemeInfo( 35 | ResourceDictionaryLocation.None, //主题特定资源词典所处位置 36 | //(未在页面中找到资源时使用, 37 | //或应用程序资源字典中找到时使用) 38 | ResourceDictionaryLocation.SourceAssembly //常规资源词典所处位置 39 | //(未在页面中找到资源时使用, 40 | //、应用程序或任何主题专用资源字典中找到时使用) 41 | )] 42 | 43 | 44 | // 程序集的版本信息由下列四个值组成: 45 | // 46 | // 主版本 47 | // 次版本 48 | // 生成号 49 | // 修订号 50 | // 51 | //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 52 | //通过使用 "*",如下所示: 53 | // [assembly: AssemblyVersion("1.0.*")] 54 | [assembly: AssemblyVersion("1.0.0.0")] 55 | [assembly: AssemblyFileVersion("1.0.0.0")] 56 | -------------------------------------------------------------------------------- /src/Chapter2.NewDocument/Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // 此代码由工具生成。 4 | // 运行时版本: 4.0.30319.42000 5 | // 6 | // 对此文件的更改可能导致不正确的行为,如果 7 | // 重新生成代码,则所做更改将丢失。 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | 12 | namespace Chapter2.NewDocument.Properties 13 | { 14 | /// 15 | /// 强类型资源类,用于查找本地化字符串等。 16 | /// 17 | // 此类是由 StronglyTypedResourceBuilder 18 | // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 19 | // 若要添加或删除成员,请编辑 .ResX 文件,然后重新运行 ResGen 20 | // (以 /str 作为命令选项),或重新生成 VS 项目。 21 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 22 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 23 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 24 | internal class Resources 25 | { 26 | 27 | private static global::System.Resources.ResourceManager resourceMan; 28 | 29 | private static global::System.Globalization.CultureInfo resourceCulture; 30 | 31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 32 | internal Resources() 33 | { 34 | } 35 | 36 | /// 37 | /// 返回此类使用的缓存 ResourceManager 实例。 38 | /// 39 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 40 | internal static global::System.Resources.ResourceManager ResourceManager 41 | { 42 | get 43 | { 44 | if ((resourceMan == null)) 45 | { 46 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Chapter2.NewDocument.Properties.Resources", typeof(Resources).Assembly); 47 | resourceMan = temp; 48 | } 49 | return resourceMan; 50 | } 51 | } 52 | 53 | /// 54 | /// 重写当前线程的 CurrentUICulture 属性,对 55 | /// 使用此强类型资源类的所有资源查找执行重写。 56 | /// 57 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 58 | internal static global::System.Globalization.CultureInfo Culture 59 | { 60 | get 61 | { 62 | return resourceCulture; 63 | } 64 | set 65 | { 66 | resourceCulture = value; 67 | } 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/Chapter2.NewDocument/Properties/Resources.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | text/microsoft-resx 107 | 108 | 109 | 2.0 110 | 111 | 112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 113 | 114 | 115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | -------------------------------------------------------------------------------- /src/Chapter2.NewDocument/Properties/Settings.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.42000 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | 12 | namespace Chapter2.NewDocument.Properties 13 | { 14 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 15 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] 16 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase 17 | { 18 | 19 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); 20 | 21 | public static Settings Default 22 | { 23 | get 24 | { 25 | return defaultInstance; 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Chapter2.NewDocument/Properties/Settings.settings: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/Chapter2.NewDocument/Readme.md: -------------------------------------------------------------------------------- 1 |  2 | -------------------------------------------------------------------------------- /src/Chapter2.NewDocument/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /src/Chapter3.PartAutomation/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/Chapter3.PartAutomation/App.xaml: -------------------------------------------------------------------------------- 1 |  6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /src/Chapter3.PartAutomation/App.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Configuration; 4 | using System.Data; 5 | using System.Linq; 6 | using System.Threading.Tasks; 7 | using System.Windows; 8 | 9 | namespace Chapter3.PartAutomation 10 | { 11 | /// 12 | /// App.xaml 的交互逻辑 13 | /// 14 | public partial class App : Application 15 | { 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/Chapter3.PartAutomation/Chapter3.PartAutomation.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {8D6E1610-0569-4675-9552-19A0893EA4F2} 8 | WinExe 9 | Chapter3.PartAutomation 10 | Chapter3.PartAutomation 11 | v4.7.2 12 | 512 13 | {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 14 | 4 15 | true 16 | true 17 | 18 | 19 | 20 | 21 | AnyCPU 22 | true 23 | full 24 | false 25 | bin\Debug\ 26 | DEBUG;TRACE 27 | prompt 28 | 4 29 | 30 | 31 | AnyCPU 32 | pdbonly 33 | true 34 | bin\Release\ 35 | TRACE 36 | prompt 37 | 4 38 | 39 | 40 | 41 | ..\packages\Xarial.XCad.SolidWorks.Interops.0.3.0\lib\net40\SolidWorks.Interop.sldworks.dll 42 | False 43 | 44 | 45 | ..\packages\Xarial.XCad.SolidWorks.Interops.0.3.0\lib\net40\SolidWorks.Interop.swconst.dll 46 | False 47 | 48 | 49 | ..\packages\Xarial.XCad.SolidWorks.Interops.0.3.0\lib\net40\SolidWorks.Interop.swpublished.dll 50 | False 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 4.0 63 | 64 | 65 | 66 | 67 | 68 | 69 | ..\packages\Xarial.XCad.0.6.10\lib\net461\Xarial.XCad.dll 70 | 71 | 72 | ..\packages\Xarial.XCad.SolidWorks.0.6.10\lib\net461\Xarial.XCad.SolidWorks.dll 73 | 74 | 75 | ..\packages\Xarial.XCad.Toolkit.0.6.10\lib\net461\Xarial.XCad.Toolkit.dll 76 | 77 | 78 | 79 | 80 | MSBuild:Compile 81 | Designer 82 | 83 | 84 | MSBuild:Compile 85 | Designer 86 | 87 | 88 | App.xaml 89 | Code 90 | 91 | 92 | 93 | 94 | MainWindow.xaml 95 | Code 96 | 97 | 98 | 99 | 100 | Code 101 | 102 | 103 | True 104 | True 105 | Resources.resx 106 | 107 | 108 | True 109 | Settings.settings 110 | True 111 | 112 | 113 | ResXFileCodeGenerator 114 | Resources.Designer.cs 115 | 116 | 117 | 118 | SettingsSingleFileGenerator 119 | Settings.Designer.cs 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。 131 | 132 | 133 | 134 | -------------------------------------------------------------------------------- /src/Chapter3.PartAutomation/Extension/IModelDocExtension.cs: -------------------------------------------------------------------------------- 1 | using SolidWorks.Interop.sldworks; 2 | using System; 3 | using System.Collections.Generic; 4 | 5 | namespace Chapter3.PartAutomation.Extension 6 | { 7 | public static class IModelDocExtension 8 | { 9 | /// 10 | /// 通过顺序获取初始基准面 11 | /// 12 | public static IEnumerable GetRefPlane(this IModelDoc2 doc) 13 | { 14 | //创建这个草图,需要找到一个基准面 15 | var feat = doc.FirstFeature() as IFeature; 16 | 17 | while (feat != null) 18 | { 19 | var name = feat.GetTypeName2(); 20 | if (name == "RefPlane") 21 | { 22 | yield return feat; 23 | } 24 | 25 | feat = feat.GetNextFeature() as IFeature; 26 | } 27 | } 28 | 29 | /// 30 | /// 在界面不刷新的情况下执行 31 | /// 32 | public static void WithNoRefresh(this IModelDoc2 doc, Action action) 33 | { 34 | if (doc is null) 35 | { 36 | throw new ArgumentNullException(nameof(doc)); 37 | } 38 | var activeView = doc.ActiveView as IModelView; 39 | var featMgr = doc.FeatureManager; 40 | try 41 | { 42 | activeView.EnableGraphicsUpdate = false; 43 | 44 | featMgr.EnableFeatureTree = false; 45 | featMgr.EnableFeatureTreeWindow = false; 46 | 47 | action?.Invoke(); 48 | } 49 | catch (Exception) 50 | { 51 | 52 | throw; 53 | } 54 | finally 55 | { 56 | activeView.EnableGraphicsUpdate = true; 57 | featMgr.EnableFeatureTree = true; 58 | featMgr.EnableFeatureTreeWindow = true; 59 | } 60 | } 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/Chapter3.PartAutomation/Extension/ISldWorksExtension.cs: -------------------------------------------------------------------------------- 1 | using SolidWorks.Interop.sldworks; 2 | using SolidWorks.Interop.swconst; 3 | using System; 4 | 5 | namespace Chapter3.PartAutomation.Extension 6 | { 7 | public static class ISldWorksExtension 8 | { 9 | /// 10 | /// 在某个设置的状态下执行动作 11 | /// 12 | /// 用户设置枚举 13 | /// 期望状态 14 | /// 执行的动作 15 | public static void WithToggleState(this ISldWorks sw,swUserPreferenceToggle_e swUserPreference, bool desState, Action action) 16 | { 17 | //先获取用户设置的初始状态 18 | var sourceState = sw.GetUserPreferenceToggle((int)swUserPreference); 19 | 20 | //设置程序需要的状态 21 | sw.SetUserPreferenceToggle((int)swUserPreference, desState); 22 | 23 | //执行我们需要的操作 24 | action?.Invoke(); 25 | 26 | //还原用户设置 27 | sw.SetUserPreferenceToggle((int)swUserPreference, sourceState); 28 | 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Chapter3.PartAutomation/MainWindow.xaml: -------------------------------------------------------------------------------- 1 |  9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 起点:0,0 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /src/Chapter3.PartAutomation/MainWindow.xaml.cs: -------------------------------------------------------------------------------- 1 | using SolidWorks.Interop.sldworks; 2 | using System.Diagnostics; 3 | using System.Linq; 4 | using System.Windows; 5 | using Xarial.XCad.SolidWorks; 6 | using SolidWorks.Interop.swconst; 7 | using System.Collections; 8 | using System.Collections.Generic; 9 | using System; 10 | using Chapter3.PartAutomation.Extension; 11 | 12 | namespace Chapter3.PartAutomation 13 | { 14 | /// 15 | /// MainWindow.xaml 的交互逻辑 16 | /// 17 | public partial class MainWindow : Window 18 | { 19 | private ISwApplication _swApp; 20 | 21 | public MainWindow() 22 | { 23 | InitializeComponent(); 24 | } 25 | 26 | /// 27 | /// 连接SolidWorks 28 | /// 29 | private void Button_Click_1(object sender, RoutedEventArgs e) 30 | { 31 | var swProcess = Process.GetProcessesByName("SLDWORKS"); 32 | if (!swProcess.Any()) 33 | { 34 | msg.Text = "没有打开SolidWorks,请打开SolidWorks后再试"; 35 | return; 36 | } 37 | 38 | _swApp = SwApplicationFactory.FromProcess(swProcess.First()); 39 | 40 | msg.Text = _swApp.Version.ToString(); 41 | } 42 | 43 | /// 44 | /// 新建一个草图并且绘制一条直线 45 | /// 46 | private void Button_Click(object sender, RoutedEventArgs e) 47 | { 48 | if (_swApp == null) 49 | { 50 | MessageBox.Show("未连接SolidWorks,请先连接SolidWorks"); 51 | return; 52 | } 53 | 54 | //获取当前打开的文档 55 | var doc = _swApp.Sw.IActiveDoc2; 56 | 57 | //判断一下执行条件 58 | if (doc == null) 59 | { 60 | MessageBox.Show("没有打开的文档"); 61 | return; 62 | } 63 | 64 | if (doc.GetType() != (int)swDocumentTypes_e.swDocPART) 65 | { 66 | MessageBox.Show("当前打开的不是零件"); 67 | return; 68 | } 69 | 70 | //获取前视基准面 71 | IFeature refFeat = doc.GetRefPlane().FirstOrDefault(); 72 | 73 | //选中这个基准面 74 | refFeat.Select2(false, 0); 75 | 76 | //在这个基准面上插入一个草图 77 | var skeMgr = doc.SketchManager; 78 | skeMgr.InsertSketch(true); 79 | 80 | var ske = skeMgr.ActiveSketch; 81 | 82 | //绘制一条直线 83 | _swApp.Sw.WithToggleState(swUserPreferenceToggle_e.swSketchInference, false, () => 84 | { 85 | skeMgr.CreateLine(0, 0, 0, double.Parse(_xPostion.Text) / 1000, double.Parse(_yPostion.Text) / 1000, 0); 86 | }); 87 | 88 | //缩放选择内容到何时大小 89 | doc.ViewZoomToSelection(); 90 | 91 | //退出草图 92 | skeMgr.InsertSketch(true); 93 | 94 | ((IFeature)ske).Name = "草图名称"; 95 | } 96 | 97 | /// 98 | /// 执行拉伸凸台 99 | /// 100 | private void CreateFeatureClick(object sender, RoutedEventArgs e) 101 | { 102 | if (_swApp ==null) 103 | { 104 | MessageBox.Show("未连接SolidWorks"); 105 | return; 106 | } 107 | 108 | var doc = _swApp.Sw.ActiveDoc as IModelDoc2; 109 | 110 | if (doc == null) 111 | { 112 | MessageBox.Show("没有打开的文档"); 113 | return; 114 | } 115 | 116 | if (doc.GetType() != (int)swDocumentTypes_e.swDocPART) 117 | { 118 | MessageBox.Show("当前打开不少零件类型的文档"); 119 | return; 120 | } 121 | 122 | doc.WithNoRefresh(() => 123 | { 124 | //早绑定到FeatureManager 125 | var featMgr = doc.FeatureManager; 126 | 127 | //新建拉伸特征 128 | var frontPlane = doc.GetRefPlane().First(); 129 | frontPlane.Select2(false, 0); 130 | 131 | var skeMgr = doc.SketchManager; 132 | skeMgr.InsertSketch(true); 133 | 134 | //绘制一个中心矩形 135 | double width = double.Parse(_rectWidth.Text) / 2; 136 | _swApp.Sw.WithToggleState(swUserPreferenceToggle_e.swSketchInference, false, () => 137 | { 138 | skeMgr.CreateCenterRectangle(0, 0, 0, width, width, 0); 139 | }); 140 | 141 | 142 | double depth = double.Parse(_depth.Text); 143 | featMgr.FeatureExtrusion3( 144 | Sd: true,//单向拉伸 145 | Flip: false, 146 | Dir: false, 147 | T1: (int)swEndConditions_e.swEndCondBlind, 148 | T2: (int)swEndConditions_e.swEndCondBlind, 149 | D1: depth, 150 | D2: 0, 151 | //拔模参数 152 | Dchk1: false, 153 | Dchk2: false, 154 | Ddir1: false, 155 | Ddir2: false, 156 | Dang1: 0, 157 | Dang2: 0, 158 | // 159 | OffsetReverse1: false, 160 | OffsetReverse2: false, 161 | TranslateSurface1: false, 162 | TranslateSurface2: false, 163 | //实体和选择 164 | Merge: true, 165 | UseFeatScope: true, 166 | UseAutoSelect: true, 167 | //起始条件 168 | T0: (int)swStartConditions_e.swStartSketchPlane, 169 | StartOffset: 0, 170 | FlipStartOffset: false); 171 | 172 | }); 173 | 174 | } 175 | 176 | private void GetSelectionInfo_BtnClick(object sender, RoutedEventArgs e) 177 | { 178 | if (_swApp == null) 179 | { 180 | MessageBox.Show("当前未连接SolidWorks"); 181 | return; 182 | } 183 | 184 | var doc = _swApp.Sw.IActiveDoc2; 185 | if (doc == null) 186 | { 187 | _swApp.ShowMessageBox("当前未打开文档", Xarial.XCad.Base.Enums.MessageBoxIcon_e.Warning); 188 | return; 189 | } 190 | 191 | //获取选择管理器 192 | var seleMgr = doc.ISelectionManager; 193 | //获取选择数量 194 | var count = seleMgr.GetSelectedObjectCount2(-1); 195 | if (count < 1) 196 | { 197 | MessageBox.Show("当前没有任何选择"); 198 | } 199 | 200 | var data = new List(count); 201 | 202 | for (int i = 1; i < count+1; i++) 203 | { 204 | var mark = seleMgr.GetSelectedObjectMark(i); 205 | var type = seleMgr.GetSelectedObjectType3(i, mark); 206 | var obj = seleMgr.GetSelectedObject6(i, mark); 207 | 208 | //选择的位置 209 | var selePostioon = seleMgr.GetSelectionPoint2(i, mark) as double[]; 210 | string info = selePostioon == null ? $"Index:{i},Mark:{mark},Type:{(swSelectType_e)type}" : 211 | $"Index:{i},Mark:{mark},Type:{(swSelectType_e)type},Postion:{selePostioon[0]},{selePostioon[1]},{selePostioon[2]}"; 212 | 213 | if (obj is IFeature feat) 214 | { 215 | var featInfo = $"特征名称{feat.Name},特征类型:{feat.GetTypeName2()}"; 216 | 217 | info += featInfo; 218 | } 219 | 220 | data.Add(info); 221 | } 222 | 223 | _selectionList.ItemsSource = data; 224 | } 225 | 226 | private void GetCusProperties_BtnClick(object sender, RoutedEventArgs e) 227 | { 228 | if (_swApp == null) 229 | { 230 | MessageBox.Show("当前未连接SolidWorks"); 231 | return; 232 | } 233 | 234 | var doc = _swApp.Sw.IActiveDoc2; 235 | if (doc == null) 236 | { 237 | _swApp.ShowMessageBox("当前未打开文档", Xarial.XCad.Base.Enums.MessageBoxIcon_e.Warning); 238 | return; 239 | } 240 | 241 | var cusMgr = doc.Extension.CustomPropertyManager[""]; 242 | 243 | object names = new object(), 244 | values = new object(), 245 | types = new object(), 246 | resolved = new object(), 247 | links = new object(); 248 | 249 | cusMgr.GetAll3(ref names, ref types, ref values, ref resolved, ref links); 250 | 251 | var cusNames = names as string[]; 252 | var cusValues = values as string[]; 253 | var cusTypes = (types as int[]).Select(p => (swCustomInfoType_e)p).ToArray(); 254 | 255 | var list = new List(); 256 | 257 | for (int i = 0; i < cusNames.Length; i++) 258 | { 259 | var name = cusNames[i]; 260 | var type = cusTypes[i]; 261 | var value = cusValues[i]; 262 | 263 | var info = $"Name:{name},Type:{type},Value:{value}"; 264 | list.Add(info); 265 | } 266 | _propertiesList.ItemsSource = list; 267 | } 268 | 269 | private void AddCusProperty_BtnClick(object sender, RoutedEventArgs e) 270 | { 271 | if (_swApp == null) 272 | { 273 | MessageBox.Show("当前未连接SolidWorks"); 274 | return; 275 | } 276 | 277 | var doc = _swApp.Sw.IActiveDoc2; 278 | if (doc == null) 279 | { 280 | _swApp.ShowMessageBox("当前未打开文档", Xarial.XCad.Base.Enums.MessageBoxIcon_e.Warning); 281 | return; 282 | } 283 | 284 | var cusMgr = doc.Extension.CustomPropertyManager[""]; 285 | 286 | var result = cusMgr.Add3("Test", (int)swCustomInfoType_e.swCustomInfoText, "Test", (int)swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd); 287 | var addResult = (swCustomInfoAddResult_e)result; 288 | 289 | if (addResult != swCustomInfoAddResult_e.swCustomInfoAddResult_AddedOrChanged) 290 | { 291 | MessageBox.Show($"添加失败,失败原因:{addResult}"); 292 | } 293 | } 294 | 295 | private void DeleteCusProperty_BtnClick(object sender, RoutedEventArgs e) 296 | { 297 | if (_swApp == null) 298 | { 299 | MessageBox.Show("当前未连接SolidWorks"); 300 | return; 301 | } 302 | 303 | var doc = _swApp.Sw.IActiveDoc2; 304 | if (doc == null) 305 | { 306 | _swApp.ShowMessageBox("当前未打开文档", Xarial.XCad.Base.Enums.MessageBoxIcon_e.Warning); 307 | return; 308 | } 309 | 310 | var cusMgr = doc.Extension.CustomPropertyManager[""]; 311 | 312 | var result =(swCustomInfoDeleteResult_e)cusMgr.Delete2("Test"); 313 | 314 | if (result != swCustomInfoDeleteResult_e.swCustomInfoDeleteResult_OK) 315 | { 316 | MessageBox.Show($"删除自定义属性:Test 失败,原因:{result}"); 317 | } 318 | } 319 | 320 | private void GetEquation_BtnClick(object sender, RoutedEventArgs e) 321 | { 322 | if (_swApp == null) 323 | { 324 | MessageBox.Show("当前未连接SolidWorks"); 325 | return; 326 | } 327 | 328 | var doc = _swApp.Sw.IActiveDoc2; 329 | if (doc == null) 330 | { 331 | _swApp.ShowMessageBox("当前未打开文档", Xarial.XCad.Base.Enums.MessageBoxIcon_e.Warning); 332 | return; 333 | } 334 | 335 | //获取方程式管理 336 | var equMgr = doc.GetEquationMgr(); 337 | 338 | var count = equMgr.GetCount(); 339 | 340 | List equations = new List(count); 341 | 342 | for (int i = 0; i < count; i++) 343 | { 344 | var equation = equMgr.Equation[i]; 345 | equations.Add(equation); 346 | } 347 | 348 | _equationList.ItemsSource = equations; 349 | } 350 | } 351 | } 352 | -------------------------------------------------------------------------------- /src/Chapter3.PartAutomation/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Resources; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.InteropServices; 5 | using System.Windows; 6 | 7 | // 有关程序集的一般信息由以下 8 | // 控制。更改这些特性值可修改 9 | // 与程序集关联的信息。 10 | [assembly: AssemblyTitle("Chapter3.PartAutomation")] 11 | [assembly: AssemblyDescription("")] 12 | [assembly: AssemblyConfiguration("")] 13 | [assembly: AssemblyCompany("")] 14 | [assembly: AssemblyProduct("Chapter3.PartAutomation")] 15 | [assembly: AssemblyCopyright("Copyright © 2021")] 16 | [assembly: AssemblyTrademark("")] 17 | [assembly: AssemblyCulture("")] 18 | 19 | // 将 ComVisible 设置为 false 会使此程序集中的类型 20 | //对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 21 | //请将此类型的 ComVisible 特性设置为 true。 22 | [assembly: ComVisible(false)] 23 | 24 | //若要开始生成可本地化的应用程序,请设置 25 | //.csproj 文件中的 CultureYouAreCodingWith 26 | //例如,如果您在源文件中使用的是美国英语, 27 | //使用的是美国英语,请将 设置为 en-US。 然后取消 28 | //对以下 NeutralResourceLanguage 特性的注释。 更新 29 | //以下行中的“en-US”以匹配项目文件中的 UICulture 设置。 30 | 31 | //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] 32 | 33 | 34 | [assembly: ThemeInfo( 35 | ResourceDictionaryLocation.None, //主题特定资源词典所处位置 36 | //(未在页面中找到资源时使用, 37 | //或应用程序资源字典中找到时使用) 38 | ResourceDictionaryLocation.SourceAssembly //常规资源词典所处位置 39 | //(未在页面中找到资源时使用, 40 | //、应用程序或任何主题专用资源字典中找到时使用) 41 | )] 42 | 43 | 44 | // 程序集的版本信息由下列四个值组成: 45 | // 46 | // 主版本 47 | // 次版本 48 | // 生成号 49 | // 修订号 50 | // 51 | //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 52 | //通过使用 "*",如下所示: 53 | // [assembly: AssemblyVersion("1.0.*")] 54 | [assembly: AssemblyVersion("1.0.0.0")] 55 | [assembly: AssemblyFileVersion("1.0.0.0")] 56 | -------------------------------------------------------------------------------- /src/Chapter3.PartAutomation/Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // 此代码由工具生成。 4 | // 运行时版本: 4.0.30319.42000 5 | // 6 | // 对此文件的更改可能导致不正确的行为,如果 7 | // 重新生成代码,则所做更改将丢失。 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | 12 | namespace Chapter3.PartAutomation.Properties 13 | { 14 | /// 15 | /// 强类型资源类,用于查找本地化字符串等。 16 | /// 17 | // 此类是由 StronglyTypedResourceBuilder 18 | // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 19 | // 若要添加或删除成员,请编辑 .ResX 文件,然后重新运行 ResGen 20 | // (以 /str 作为命令选项),或重新生成 VS 项目。 21 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 22 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 23 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 24 | internal class Resources 25 | { 26 | 27 | private static global::System.Resources.ResourceManager resourceMan; 28 | 29 | private static global::System.Globalization.CultureInfo resourceCulture; 30 | 31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 32 | internal Resources() 33 | { 34 | } 35 | 36 | /// 37 | /// 返回此类使用的缓存 ResourceManager 实例。 38 | /// 39 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 40 | internal static global::System.Resources.ResourceManager ResourceManager 41 | { 42 | get 43 | { 44 | if ((resourceMan == null)) 45 | { 46 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Chapter3.PartAutomation.Properties.Resources", typeof(Resources).Assembly); 47 | resourceMan = temp; 48 | } 49 | return resourceMan; 50 | } 51 | } 52 | 53 | /// 54 | /// 重写当前线程的 CurrentUICulture 属性,对 55 | /// 使用此强类型资源类的所有资源查找执行重写。 56 | /// 57 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 58 | internal static global::System.Globalization.CultureInfo Culture 59 | { 60 | get 61 | { 62 | return resourceCulture; 63 | } 64 | set 65 | { 66 | resourceCulture = value; 67 | } 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/Chapter3.PartAutomation/Properties/Resources.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | text/microsoft-resx 107 | 108 | 109 | 2.0 110 | 111 | 112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 113 | 114 | 115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | -------------------------------------------------------------------------------- /src/Chapter3.PartAutomation/Properties/Settings.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.42000 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | 12 | namespace Chapter3.PartAutomation.Properties 13 | { 14 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 15 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] 16 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase 17 | { 18 | 19 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); 20 | 21 | public static Settings Default 22 | { 23 | get 24 | { 25 | return defaultInstance; 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Chapter3.PartAutomation/Properties/Settings.settings: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/Chapter3.PartAutomation/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/Chapter3.PartAutomation/readme.md: -------------------------------------------------------------------------------- 1 | ```csharp 2 | /// 3 | /// 创建一个拉伸凸台特征 4 | /// 5 | private void CreateFeatureClick(object sender, RoutedEventArgs e) 6 | { 7 | if (_swApp ==null) 8 | { 9 | MessageBox.Show("当前未连接到SolidWorks"); 10 | return; 11 | } 12 | 13 | var doc = _swApp.Sw.ActiveDoc as IModelDoc2; 14 | 15 | //判断有没有打开文档 16 | if (doc == null) 17 | { 18 | MessageBox.Show("当前无活动文档"); 19 | return; 20 | } 21 | 22 | if (doc.GetType() != (int)swDocumentTypes_e.swDocPART) 23 | { 24 | MessageBox.Show($"当前打开的不是零件,而是{(swDocumentTypes_e)doc.GetType()}"); 25 | return; 26 | } 27 | 28 | //获取前视基准面特征 29 | var frontPlane = doc.GetRefPlane().First(); 30 | 31 | frontPlane.Select2(false, 0); 32 | 33 | //在基准面上创建草图 34 | var skeMgr = doc.SketchManager; 35 | skeMgr.InsertSketch(true); 36 | 37 | var ske = skeMgr.ActiveSketch; 38 | //绘制矩形 39 | _swApp.Sw.WithToggleState(swUserPreferenceToggle_e.swSketchInference, false, () => 40 | { 41 | var segs = (skeMgr.CreateCenterRectangle(0, 0, 0, 0.01, 0.01, 0) as object[]).Cast(); 42 | }); 43 | 44 | //doc.EditRebuild3(); 45 | //((IFeature)ske).Select2(false, 0); 46 | 47 | //直接新建拉伸特征 48 | var featMgr = doc.FeatureManager; 49 | featMgr.FeatureExtrusion3( 50 | Sd: true, 51 | Flip: false, 52 | Dir: false, 53 | T1: (int)swEndConditions_e.swEndCondBlind, 54 | T2: (int)swEndConditions_e.swEndCondBlind, 55 | D1: 0.01, 56 | D2: 0, 57 | Dchk1: false, 58 | Dchk2: false, 59 | Ddir1: false, 60 | Ddir2: false, 61 | Dang1: 0, 62 | Dang2: 0, 63 | OffsetReverse1: false, 64 | OffsetReverse2: false, 65 | TranslateSurface1: false, 66 | TranslateSurface2: false, 67 | Merge: true, 68 | UseFeatScope: true, 69 | UseAutoSelect: true, 70 | T0: (int)swStartConditions_e.swStartSketchPlane, 71 | StartOffset: 0, 72 | FlipStartOffset: false); 73 | } 74 | ``` 75 | 76 | ```csharp 77 | /// 执行代码时不刷新图像区域 和 特征树 78 | public static void WithNoRefresh(this IModelDoc2 doc, Action action) 79 | { 80 | if (doc is null) 81 | { 82 | throw new ArgumentNullException(nameof(doc)); 83 | } 84 | 85 | IModelView swView = null; 86 | try 87 | { 88 | swView = doc.ActiveView as IModelView; 89 | swView.SuppressWaitCursorDuringRedraw = true; 90 | swView.EnableGraphicsUpdate = false; 91 | doc.FeatureManager.EnableFeatureTreeWindow = false; 92 | doc.FeatureManager.EnableFeatureTree = false; 93 | 94 | doc.Lock(); 95 | 96 | action?.Invoke(); 97 | } 98 | catch (Exception) 99 | { 100 | 101 | throw; 102 | } 103 | finally 104 | { 105 | doc.UnLock(); 106 | 107 | doc.FeatureManager.EnableFeatureTreeWindow = true; 108 | doc.FeatureManager.EnableFeatureTree = true; 109 | 110 | swView.EnableGraphicsUpdate = true; 111 | swView.SuppressWaitCursorDuringRedraw = false; 112 | } 113 | } 114 | ``` -------------------------------------------------------------------------------- /src/Chapter4.MyFirstAddin/Chapter4.MyFirstAddin.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {A0A3465C-0D3C-4B0D-A416-CDB1F2651255} 8 | Library 9 | Properties 10 | Chapter4.MyFirstAddin 11 | Chapter4.MyFirstAddin 12 | v4.7.2 13 | 512 14 | true 15 | 16 | 17 | 18 | 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | 38 | 39 | ..\packages\Xarial.XCad.SolidWorks.Interops.0.3.0\lib\net40\SolidWorks.Interop.sldworks.dll 40 | False 41 | 42 | 43 | ..\packages\Xarial.XCad.SolidWorks.Interops.0.3.0\lib\net40\SolidWorks.Interop.swconst.dll 44 | False 45 | 46 | 47 | ..\packages\Xarial.XCad.SolidWorks.Interops.0.3.0\lib\net40\SolidWorks.Interop.swpublished.dll 48 | False 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | ..\packages\Xarial.XCad.0.7.0\lib\net461\Xarial.XCad.dll 64 | 65 | 66 | ..\packages\Xarial.XCad.SolidWorks.0.7.0\lib\net461\Xarial.XCad.SolidWorks.dll 67 | 68 | 69 | ..\packages\Xarial.XCad.Toolkit.0.7.0\lib\net461\Xarial.XCad.Toolkit.dll 70 | 71 | 72 | 73 | 74 | 75 | 76 | True 77 | True 78 | Resource.resx 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | PreserveNewest 87 | 88 | 89 | 90 | 91 | 92 | ResXFileCodeGenerator 93 | Resource.Designer.cs 94 | 95 | 96 | 97 | 98 | 99 | 100 | 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。 101 | 102 | 103 | 104 | -------------------------------------------------------------------------------- /src/Chapter4.MyFirstAddin/MyFirstAddin.cs: -------------------------------------------------------------------------------- 1 | using Chapter4.MyFirstAddin.Properties; 2 | using System.ComponentModel; 3 | using System.Runtime.InteropServices; 4 | using Xarial.XCad.Base.Attributes; 5 | using Xarial.XCad.SolidWorks; 6 | using Xarial.XCad.UI.Commands; 7 | 8 | namespace Chapter4.MyFirstAddin 9 | { 10 | /* 11 | * 1.注册插件 12 | * 为了让SolidWorks知道我们插件的路径 13 | * 需要用到 RegAsm.exe 注册插件 14 | * 如何找到RegAsm.exe,C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe 15 | * 16 | * RegAsm.exe [程序集名称].dll /codebase 17 | * 18 | * 2.卸载插件 19 | * RegAsm.exe [程序集名称].dll /u 20 | * 21 | * 3.xCAD 已经为我们封装了上面的注册和卸载过程,我们不需要去手动去注册和卸载; 22 | */ 23 | 24 | [ComVisible(true)] 25 | [Title("第一个插件")] 26 | public class MyFirstAddin:SwAddInEx 27 | { 28 | public override void OnConnect() 29 | { 30 | var cmdGroup = CommandManager.AddCommandGroup(); 31 | } 32 | } 33 | 34 | public enum Commands 35 | { 36 | [Title("第一个按钮")] 37 | [Description("我的第一个按钮")] 38 | [Icon(typeof(Resource),nameof(Resource.MyFirstCommand))] 39 | FirstCommand 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Chapter4.MyFirstAddin/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的一般信息由以下 6 | // 控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("Chapter4.MyFirstAddin")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Chapter4.MyFirstAddin")] 13 | [assembly: AssemblyCopyright("Copyright © 2021")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // 将 ComVisible 设置为 false 会使此程序集中的类型 18 | //对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 19 | //请将此类型的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("a0a3465c-0d3c-4b0d-a416-cdb1f2651255")] 24 | 25 | // 程序集的版本信息由下列四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 生成号 30 | // 修订号 31 | // 32 | //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 33 | //通过使用 "*",如下所示: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /src/Chapter4.MyFirstAddin/Properties/Resource.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // 此代码由工具生成。 4 | // 运行时版本:4.0.30319.42000 5 | // 6 | // 对此文件的更改可能会导致不正确的行为,并且如果 7 | // 重新生成代码,这些更改将会丢失。 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace Chapter4.MyFirstAddin.Properties { 12 | using System; 13 | 14 | 15 | /// 16 | /// 一个强类型的资源类,用于查找本地化的字符串等。 17 | /// 18 | // 此类是由 StronglyTypedResourceBuilder 19 | // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 20 | // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen 21 | // (以 /str 作为命令选项),或重新生成 VS 项目。 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class Resource { 26 | 27 | private static global::System.Resources.ResourceManager resourceMan; 28 | 29 | private static global::System.Globalization.CultureInfo resourceCulture; 30 | 31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 32 | internal Resource() { 33 | } 34 | 35 | /// 36 | /// 返回此类使用的缓存的 ResourceManager 实例。 37 | /// 38 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 39 | internal static global::System.Resources.ResourceManager ResourceManager { 40 | get { 41 | if (object.ReferenceEquals(resourceMan, null)) { 42 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Chapter4.MyFirstAddin.Properties.Resource", typeof(Resource).Assembly); 43 | resourceMan = temp; 44 | } 45 | return resourceMan; 46 | } 47 | } 48 | 49 | /// 50 | /// 重写当前线程的 CurrentUICulture 属性,对 51 | /// 使用此强类型资源类的所有资源查找执行重写。 52 | /// 53 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 54 | internal static global::System.Globalization.CultureInfo Culture { 55 | get { 56 | return resourceCulture; 57 | } 58 | set { 59 | resourceCulture = value; 60 | } 61 | } 62 | 63 | /// 64 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 65 | /// 66 | internal static System.Drawing.Bitmap MyFirstCommand { 67 | get { 68 | object obj = ResourceManager.GetObject("MyFirstCommand", resourceCulture); 69 | return ((System.Drawing.Bitmap)(obj)); 70 | } 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/Chapter4.MyFirstAddin/Properties/Resource.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | 121 | 122 | ..\resource\myfirstcommand.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 123 | 124 | -------------------------------------------------------------------------------- /src/Chapter4.MyFirstAddin/RegAsm.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weianweigan/SolidWorksAPITutorial/147432e8bf6ed1499849de78448ad307264179f6/src/Chapter4.MyFirstAddin/RegAsm.exe -------------------------------------------------------------------------------- /src/Chapter4.MyFirstAddin/Resource/MyFirstCommand.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weianweigan/SolidWorksAPITutorial/147432e8bf6ed1499849de78448ad307264179f6/src/Chapter4.MyFirstAddin/Resource/MyFirstCommand.png -------------------------------------------------------------------------------- /src/Chapter4.MyFirstAddin/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/SolidWorksTutorial.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.30804.86 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chapter1.ConnectToSw", "Chapter1.ConnectToSw\Chapter1.ConnectToSw.csproj", "{431230E1-4EB7-4205-928F-F91B8A1BD259}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chapter2.NewDocument", "Chapter2.NewDocument\Chapter2.NewDocument.csproj", "{482A0711-2896-4457-935B-EBBFE3EA3228}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chapter3.PartAutomation", "Chapter3.PartAutomation\Chapter3.PartAutomation.csproj", "{8D6E1610-0569-4675-9552-19A0893EA4F2}" 11 | EndProject 12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chapter4.MyFirstAddin", "Chapter4.MyFirstAddin\Chapter4.MyFirstAddin.csproj", "{A0A3465C-0D3C-4B0D-A416-CDB1F2651255}" 13 | EndProject 14 | Global 15 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 16 | Debug|Any CPU = Debug|Any CPU 17 | Debug|x64 = Debug|x64 18 | Release|Any CPU = Release|Any CPU 19 | Release|x64 = Release|x64 20 | EndGlobalSection 21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 22 | {431230E1-4EB7-4205-928F-F91B8A1BD259}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {431230E1-4EB7-4205-928F-F91B8A1BD259}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {431230E1-4EB7-4205-928F-F91B8A1BD259}.Debug|x64.ActiveCfg = Debug|x64 25 | {431230E1-4EB7-4205-928F-F91B8A1BD259}.Debug|x64.Build.0 = Debug|x64 26 | {431230E1-4EB7-4205-928F-F91B8A1BD259}.Release|Any CPU.ActiveCfg = Release|Any CPU 27 | {431230E1-4EB7-4205-928F-F91B8A1BD259}.Release|Any CPU.Build.0 = Release|Any CPU 28 | {431230E1-4EB7-4205-928F-F91B8A1BD259}.Release|x64.ActiveCfg = Release|x64 29 | {431230E1-4EB7-4205-928F-F91B8A1BD259}.Release|x64.Build.0 = Release|x64 30 | {482A0711-2896-4457-935B-EBBFE3EA3228}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 31 | {482A0711-2896-4457-935B-EBBFE3EA3228}.Debug|Any CPU.Build.0 = Debug|Any CPU 32 | {482A0711-2896-4457-935B-EBBFE3EA3228}.Debug|x64.ActiveCfg = Debug|Any CPU 33 | {482A0711-2896-4457-935B-EBBFE3EA3228}.Debug|x64.Build.0 = Debug|Any CPU 34 | {482A0711-2896-4457-935B-EBBFE3EA3228}.Release|Any CPU.ActiveCfg = Release|Any CPU 35 | {482A0711-2896-4457-935B-EBBFE3EA3228}.Release|Any CPU.Build.0 = Release|Any CPU 36 | {482A0711-2896-4457-935B-EBBFE3EA3228}.Release|x64.ActiveCfg = Release|Any CPU 37 | {482A0711-2896-4457-935B-EBBFE3EA3228}.Release|x64.Build.0 = Release|Any CPU 38 | {8D6E1610-0569-4675-9552-19A0893EA4F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 39 | {8D6E1610-0569-4675-9552-19A0893EA4F2}.Debug|Any CPU.Build.0 = Debug|Any CPU 40 | {8D6E1610-0569-4675-9552-19A0893EA4F2}.Debug|x64.ActiveCfg = Debug|Any CPU 41 | {8D6E1610-0569-4675-9552-19A0893EA4F2}.Debug|x64.Build.0 = Debug|Any CPU 42 | {8D6E1610-0569-4675-9552-19A0893EA4F2}.Release|Any CPU.ActiveCfg = Release|Any CPU 43 | {8D6E1610-0569-4675-9552-19A0893EA4F2}.Release|Any CPU.Build.0 = Release|Any CPU 44 | {8D6E1610-0569-4675-9552-19A0893EA4F2}.Release|x64.ActiveCfg = Release|Any CPU 45 | {8D6E1610-0569-4675-9552-19A0893EA4F2}.Release|x64.Build.0 = Release|Any CPU 46 | {A0A3465C-0D3C-4B0D-A416-CDB1F2651255}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 47 | {A0A3465C-0D3C-4B0D-A416-CDB1F2651255}.Debug|Any CPU.Build.0 = Debug|Any CPU 48 | {A0A3465C-0D3C-4B0D-A416-CDB1F2651255}.Debug|x64.ActiveCfg = Debug|Any CPU 49 | {A0A3465C-0D3C-4B0D-A416-CDB1F2651255}.Debug|x64.Build.0 = Debug|Any CPU 50 | {A0A3465C-0D3C-4B0D-A416-CDB1F2651255}.Release|Any CPU.ActiveCfg = Release|Any CPU 51 | {A0A3465C-0D3C-4B0D-A416-CDB1F2651255}.Release|Any CPU.Build.0 = Release|Any CPU 52 | {A0A3465C-0D3C-4B0D-A416-CDB1F2651255}.Release|x64.ActiveCfg = Release|Any CPU 53 | {A0A3465C-0D3C-4B0D-A416-CDB1F2651255}.Release|x64.Build.0 = Release|Any CPU 54 | EndGlobalSection 55 | GlobalSection(SolutionProperties) = preSolution 56 | HideSolutionNode = FALSE 57 | EndGlobalSection 58 | GlobalSection(ExtensibilityGlobals) = postSolution 59 | SolutionGuid = {AC63822C-0774-4989-A421-51FCF742F70A} 60 | EndGlobalSection 61 | EndGlobal 62 | --------------------------------------------------------------------------------