├── .gitignore ├── AutoCADCommands.sln ├── AutoCADCommands ├── Algorithms.cs ├── App.cs ├── AutoCADCommands.R18.csproj ├── AutoCADCommands.R19.csproj ├── AutoCADCommands.csproj ├── AutoCADCommands.ruleset ├── Commands.cs ├── DbHelper.cs ├── FlexDataStore.cs ├── GUIs │ ├── DictionaryViewer.xaml │ ├── DictionaryViewer.xaml.cs │ ├── InputBox.xaml │ ├── InputBox.xaml.cs │ ├── MultiInputs.xaml │ ├── MultiInputs.xaml.cs │ ├── TaskProgressWindow.xaml │ ├── TaskProgressWindow.xaml.cs │ ├── TextReport.xaml │ └── TextReport.xaml.cs ├── Gui.cs ├── Interaction.cs ├── Internal │ ├── CustomDictionary.cs │ └── JigDrag.cs ├── Layouts.cs ├── Properties │ └── AssemblyInfo.cs ├── QuickSelection.cs ├── SymbolPack.cs ├── Test.cs ├── Tuples.cs └── packages.config ├── LICENSE ├── README.md └── Reference └── AutoCAD ├── r18 ├── AcCui.dll ├── AcCui.xml ├── AcTcMgd.dll ├── AcWindows.dll ├── AdWindows.dll ├── acdbmgd.dll ├── acdbmgd.xml ├── acmgd.dll └── acmgd.xml └── r19 ├── AcCoreMgd.dll ├── AcCui.dll ├── AcDbMgd.dll ├── AcMgd.dll ├── AcTcMgd.dll ├── AcWindows.dll └── AdWindows.dll /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | artifacts/ 46 | 47 | *_i.c 48 | *_p.c 49 | *_i.h 50 | *.ilk 51 | *.meta 52 | *.obj 53 | *.pch 54 | *.pdb 55 | *.pgc 56 | *.pgd 57 | *.rsp 58 | *.sbr 59 | *.tlb 60 | *.tli 61 | *.tlh 62 | *.tmp 63 | *.tmp_proj 64 | *.log 65 | *.vspscc 66 | *.vssscc 67 | .builds 68 | *.pidb 69 | *.svclog 70 | *.scc 71 | 72 | # Chutzpah Test files 73 | _Chutzpah* 74 | 75 | # Visual C++ cache files 76 | ipch/ 77 | *.aps 78 | *.ncb 79 | *.opendb 80 | *.opensdf 81 | *.sdf 82 | *.cachefile 83 | *.VC.db 84 | *.VC.VC.opendb 85 | 86 | # Visual Studio profiler 87 | *.psess 88 | *.vsp 89 | *.vspx 90 | *.sap 91 | 92 | # TFS 2012 Local Workspace 93 | $tf/ 94 | 95 | # Guidance Automation Toolkit 96 | *.gpState 97 | 98 | # ReSharper is a .NET coding add-in 99 | _ReSharper*/ 100 | *.[Rr]e[Ss]harper 101 | *.DotSettings.user 102 | 103 | # JustCode is a .NET coding add-in 104 | .JustCode 105 | 106 | # TeamCity is a build add-in 107 | _TeamCity* 108 | 109 | # DotCover is a Code Coverage Tool 110 | *.dotCover 111 | 112 | # NCrunch 113 | _NCrunch_* 114 | .*crunch*.local.xml 115 | nCrunchTemp_* 116 | 117 | # MightyMoose 118 | *.mm.* 119 | AutoTest.Net/ 120 | 121 | # Web workbench (sass) 122 | .sass-cache/ 123 | 124 | # Installshield output folder 125 | [Ee]xpress/ 126 | 127 | # DocProject is a documentation generator add-in 128 | DocProject/buildhelp/ 129 | DocProject/Help/*.HxT 130 | DocProject/Help/*.HxC 131 | DocProject/Help/*.hhc 132 | DocProject/Help/*.hhk 133 | DocProject/Help/*.hhp 134 | DocProject/Help/Html2 135 | DocProject/Help/html 136 | 137 | # Click-Once directory 138 | publish/ 139 | 140 | # Publish Web Output 141 | *.[Pp]ublish.xml 142 | *.azurePubxml 143 | # TODO: Comment the next line if you want to checkin your web deploy settings 144 | # but database connection strings (with potential passwords) will be unencrypted 145 | *.pubxml 146 | *.publishproj 147 | 148 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 149 | # checkin your Azure Web App publish settings, but sensitive information contained 150 | # in these scripts will be unencrypted 151 | PublishScripts/ 152 | 153 | # NuGet Packages 154 | *.nupkg 155 | # The packages folder can be ignored because of Package Restore 156 | **/packages/* 157 | # except build/, which is used as an MSBuild target. 158 | !**/packages/build/ 159 | # Uncomment if necessary however generally it will be regenerated when needed 160 | #!**/packages/repositories.config 161 | # NuGet v3's project.json files produces more ignoreable files 162 | *.nuget.props 163 | *.nuget.targets 164 | 165 | # Microsoft Azure Build Output 166 | csx/ 167 | *.build.csdef 168 | 169 | # Microsoft Azure Emulator 170 | ecf/ 171 | rcf/ 172 | 173 | # Windows Store app package directories and files 174 | AppPackages/ 175 | BundleArtifacts/ 176 | Package.StoreAssociation.xml 177 | _pkginfo.txt 178 | 179 | # Visual Studio cache files 180 | # files ending in .cache can be ignored 181 | *.[Cc]ache 182 | # but keep track of directories ending in .cache 183 | !*.[Cc]ache/ 184 | 185 | # Others 186 | ClientBin/ 187 | ~$* 188 | *~ 189 | *.dbmdl 190 | *.dbproj.schemaview 191 | *.pfx 192 | *.publishsettings 193 | node_modules/ 194 | orleans.codegen.cs 195 | 196 | # Since there are multiple workflows, uncomment next line to ignore bower_components 197 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 198 | #bower_components/ 199 | 200 | # RIA/Silverlight projects 201 | Generated_Code/ 202 | 203 | # Backup & report files from converting an old project file 204 | # to a newer Visual Studio version. Backup files are not needed, 205 | # because we have git ;-) 206 | _UpgradeReport_Files/ 207 | Backup*/ 208 | UpgradeLog*.XML 209 | UpgradeLog*.htm 210 | 211 | # SQL Server files 212 | *.mdf 213 | *.ldf 214 | 215 | # Business Intelligence projects 216 | *.rdl.data 217 | *.bim.layout 218 | *.bim_*.settings 219 | 220 | # Microsoft Fakes 221 | FakesAssemblies/ 222 | 223 | # GhostDoc plugin setting file 224 | *.GhostDoc.xml 225 | 226 | # Node.js Tools for Visual Studio 227 | .ntvs_analysis.dat 228 | 229 | # Visual Studio 6 build log 230 | *.plg 231 | 232 | # Visual Studio 6 workspace options file 233 | *.opt 234 | 235 | # Visual Studio LightSwitch build output 236 | **/*.HTMLClient/GeneratedArtifacts 237 | **/*.DesktopClient/GeneratedArtifacts 238 | **/*.DesktopClient/ModelManifest.xml 239 | **/*.Server/GeneratedArtifacts 240 | **/*.Server/ModelManifest.xml 241 | _Pvt_Extensions 242 | 243 | # Paket dependency manager 244 | .paket/paket.exe 245 | paket-files/ 246 | 247 | # FAKE - F# Make 248 | .fake/ 249 | 250 | # JetBrains Rider 251 | .idea/ 252 | *.sln.iml 253 | -------------------------------------------------------------------------------- /AutoCADCommands.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.28307.572 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoCADCommands", "AutoCADCommands\AutoCADCommands.csproj", "{C5F6C326-952B-43E8-9830-311F190BAB11}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoCADCommands.R18", "AutoCADCommands\AutoCADCommands.R18.csproj", "{EDA1D1A6-3204-4B82-8D5F-4F59815F072E}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoCADCommands.R19", "AutoCADCommands\AutoCADCommands.R19.csproj", "{77B9F8FE-F75B-48C8-8249-6859E8D347E1}" 11 | EndProject 12 | Global 13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 14 | Debug|Any CPU = Debug|Any CPU 15 | Release|Any CPU = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {C5F6C326-952B-43E8-9830-311F190BAB11}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 19 | {C5F6C326-952B-43E8-9830-311F190BAB11}.Debug|Any CPU.Build.0 = Debug|Any CPU 20 | {C5F6C326-952B-43E8-9830-311F190BAB11}.Release|Any CPU.ActiveCfg = Release|Any CPU 21 | {C5F6C326-952B-43E8-9830-311F190BAB11}.Release|Any CPU.Build.0 = Release|Any CPU 22 | {EDA1D1A6-3204-4B82-8D5F-4F59815F072E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {EDA1D1A6-3204-4B82-8D5F-4F59815F072E}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {EDA1D1A6-3204-4B82-8D5F-4F59815F072E}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {EDA1D1A6-3204-4B82-8D5F-4F59815F072E}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {77B9F8FE-F75B-48C8-8249-6859E8D347E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {77B9F8FE-F75B-48C8-8249-6859E8D347E1}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {77B9F8FE-F75B-48C8-8249-6859E8D347E1}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {77B9F8FE-F75B-48C8-8249-6859E8D347E1}.Release|Any CPU.Build.0 = Release|Any CPU 30 | EndGlobalSection 31 | GlobalSection(SolutionProperties) = preSolution 32 | HideSolutionNode = FALSE 33 | EndGlobalSection 34 | GlobalSection(ExtensibilityGlobals) = postSolution 35 | SolutionGuid = {9516FD57-86E1-498D-BC03-D6EFC30A7DE6} 36 | EndGlobalSection 37 | EndGlobal 38 | -------------------------------------------------------------------------------- /AutoCADCommands/App.cs: -------------------------------------------------------------------------------- 1 | using Autodesk.AutoCAD.ApplicationServices; 2 | using Autodesk.AutoCAD.DatabaseServices; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.IO; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Reflection; 9 | using System.Runtime.InteropServices; 10 | 11 | namespace Dreambuild.AutoCAD 12 | { 13 | /// 14 | /// Application and multi-docs. 15 | /// 16 | public static class App 17 | { 18 | /// 19 | /// Gets the current folder. 20 | /// 21 | public static string CurrentFolder 22 | { 23 | get 24 | { 25 | string s = Assembly.GetCallingAssembly().Location; 26 | return s.Remove(s.LastIndexOf('\\') + 1); 27 | } 28 | } 29 | 30 | /// 31 | /// Gets the document folder. 32 | /// 33 | public static string DocumentFolder 34 | { 35 | get 36 | { 37 | string s = Application.DocumentManager.MdiActiveDocument.Name; 38 | if (s.Contains(':')) 39 | { 40 | return s.Remove(s.LastIndexOf('\\') + 1); 41 | } 42 | else 43 | { 44 | return string.Empty; 45 | } 46 | } 47 | } 48 | 49 | /// 50 | /// Loads another assembly. 51 | /// 52 | /// The assembly file name (relative path). 53 | public static void LoadDll(string dllName) 54 | { 55 | try 56 | { 57 | Assembly.LoadFrom(Path.Combine(App.CurrentFolder, dllName)); 58 | } 59 | catch 60 | { 61 | } 62 | } 63 | 64 | #region multi doc 65 | 66 | /// 67 | /// Determines if a document is newly created. 68 | /// 69 | /// The document. 70 | /// The result. 71 | public static bool IsDocumentNew(Document doc = null) // newly 20140730 72 | { 73 | return !App.IsDocumentSaved(doc ?? Application.DocumentManager.MdiActiveDocument); 74 | } 75 | 76 | /// 77 | /// Determines if a document is saved. 78 | /// 79 | /// The document. 80 | /// The result. 81 | public static bool IsDocumentSaved(Document doc = null) // newly 20140730 82 | { 83 | doc = doc ?? Application.DocumentManager.MdiActiveDocument; 84 | return doc.Name != null && doc.Name.Contains(":"); 85 | } 86 | 87 | private static IEnumerable GetAllOpenedDocInternal() 88 | { 89 | foreach (Document doc in Application.DocumentManager) 90 | { 91 | yield return doc; 92 | } 93 | } 94 | 95 | /// 96 | /// Gets all opened documents. 97 | /// 98 | /// All opened documents. 99 | public static List GetAllOpenedDocuments() 100 | { 101 | return App.GetAllOpenedDocInternal().ToList(); 102 | } 103 | 104 | /// 105 | /// Locks the current document and do things. 106 | /// 107 | /// The things to do. 108 | public static void LockAndExecute(Action action) 109 | { 110 | using (var doclock = Application.DocumentManager.MdiActiveDocument.LockDocument()) 111 | { 112 | action(); 113 | } 114 | } 115 | 116 | /// 117 | /// Locks a specified document and do things. 118 | /// 119 | /// The document to lock. 120 | /// Then things to do. 121 | public static void LockAndExecute(Document doc, Action action) 122 | { 123 | using (var doclock = doc.LockDocument()) 124 | { 125 | action(); 126 | } 127 | } 128 | 129 | /// 130 | /// Locks the current document and do things. 131 | /// 132 | /// The type of the return value. 133 | /// The things to do. 134 | /// The value returned by the action. 135 | public static T LockAndExecute(Func function) 136 | { 137 | using (var doclock = Application.DocumentManager.MdiActiveDocument.LockDocument()) 138 | { 139 | return function(); 140 | } 141 | } 142 | 143 | /// 144 | /// Sets the active document. 145 | /// 146 | /// The document to be set as active. 147 | public static void SetActiveDocument(Document doc) 148 | { 149 | Application.DocumentManager.MdiActiveDocument = doc; 150 | HostApplicationServices.WorkingDatabase = doc.Database; 151 | } 152 | 153 | /// 154 | /// Sets the working database. 155 | /// 156 | /// The database to be set as the working database. 157 | public static void SetWorkingDatabase(Database db) 158 | { 159 | HostApplicationServices.WorkingDatabase = db; 160 | } 161 | 162 | /// 163 | /// Opens a file as a document. 164 | /// 165 | /// The file name. 166 | /// The opened document. 167 | public static Document OpenDocument(string file) 168 | { 169 | return Application.DocumentManager.Open(file, false); 170 | } 171 | 172 | /// 173 | /// Opens or activates a file as a document. 174 | /// 175 | /// The file name. 176 | /// The opened document. 177 | public static Document OpenOrActivateDocument(string file) 178 | { 179 | var doc = App.FindOpenedDocument(file); 180 | if (doc != null) 181 | { 182 | App.SetActiveDocument(doc); 183 | return doc; 184 | } 185 | 186 | return App.OpenDocument(file); 187 | } 188 | 189 | /// 190 | /// Finds an opened document by file name. 191 | /// 192 | /// The file name. 193 | /// The document found. 194 | public static Document FindOpenedDocument(string file) 195 | { 196 | return Application.DocumentManager 197 | .Cast() 198 | .FirstOrDefault(document => document.Name == file); 199 | } 200 | 201 | #endregion 202 | } 203 | 204 | /// 205 | /// The polyline needs cleanup exception. 206 | /// 207 | public class PolylineNeedsCleanupException : Exception 208 | { 209 | /// 210 | /// Initializes a new instance of the exception. 211 | /// 212 | public PolylineNeedsCleanupException() 213 | { 214 | } 215 | 216 | /// 217 | /// Initializes a new instance of the exception. 218 | /// 219 | /// The message. 220 | public PolylineNeedsCleanupException(string message) : base(message) 221 | { 222 | } 223 | 224 | /// 225 | /// Initializes a new instance of the exception. 226 | /// 227 | /// The message. 228 | /// The inner exception. 229 | public PolylineNeedsCleanupException(string message, Exception innerException) : base(message, innerException) 230 | { 231 | } 232 | 233 | /// 234 | /// Shows a message dialog of this exception. 235 | /// 236 | public void ShowMessage() 237 | { 238 | Interaction.TaskDialog("Polyline needs a clean-up", "Go to clean it up.", "Go to clean it up.", "AutoCAD", "Please run `PolyClean`."); 239 | } 240 | } 241 | 242 | /// 243 | /// The log manager. 244 | /// 245 | public static class LogManager 246 | { 247 | /// 248 | /// The log file name. 249 | /// 250 | public const string LogFile = "C:\\AcadCodePack.log"; 251 | 252 | /// 253 | /// Writes object to logs. 254 | /// 255 | /// The object. 256 | public static void Write(object o) 257 | { 258 | using (var sw = new StreamWriter(LogFile, append: true)) 259 | { 260 | sw.WriteLine($"[{DateTime.Now.ToShortDateString()} {DateTime.Now.ToLongTimeString()}] {o}"); 261 | } 262 | } 263 | 264 | /// 265 | /// Writes object to logs. 266 | /// 267 | /// The note. 268 | /// The object. 269 | public static void Write(string note, object o) 270 | { 271 | using (var sw = new StreamWriter(LogFile, append: true)) 272 | { 273 | sw.WriteLine($"[{DateTime.Now.ToShortDateString()} {DateTime.Now.ToLongTimeString()}] {note}: {o}"); 274 | } 275 | } 276 | 277 | /// 278 | /// Gets a time based name. 279 | /// 280 | /// The name. 281 | public static string GetTimeBasedName() 282 | { 283 | string timeString = DateTime.Now.ToShortDateString() + "-" + DateTime.Now.ToLongTimeString(); 284 | return new string(timeString.Where(c => char.IsDigit(c)).ToArray()); 285 | } 286 | 287 | /// 288 | /// The log table. 289 | /// 290 | public class LogTable 291 | { 292 | private readonly int[] _colWidths; 293 | 294 | /// 295 | /// The tab width. 296 | /// 297 | public const int TabWidth = 8; 298 | 299 | /// 300 | /// Initializes a new instance. 301 | /// 302 | /// The column widths. The values should be multiples of the tab width. 303 | public LogTable(params int[] colWidths) 304 | { 305 | _colWidths = colWidths; 306 | } 307 | 308 | /// 309 | /// Gets the string representation of a row. 310 | /// 311 | /// The contents. 312 | /// The result. 313 | public string GetRow(params object[] contents) 314 | { 315 | var sb = new StringBuilder(); 316 | for (int i = 0; i < contents.Length; i++) 317 | { 318 | string content = contents[i].ToString(); 319 | sb.Append(content); 320 | int nTab = (int)Math.Ceiling((double)(_colWidths[i] - GetStringWidth(content)) / TabWidth); 321 | for (int j = 0; j < nTab; j++) 322 | { 323 | sb.Append('\t'); 324 | } 325 | } 326 | return sb.ToString(); 327 | } 328 | 329 | /// 330 | /// Gets string width: ASCII as 1, others as 2. 331 | /// 332 | /// The string. 333 | /// The width. 334 | public static int GetStringWidth(string content) 335 | { 336 | return content.Sum(c => c > 255 ? 2 : 1); 337 | } 338 | } 339 | } 340 | 341 | /// 342 | /// ObjectARX C functions wrapper. 343 | /// 344 | public static class Arx 345 | { 346 | [DllImport("acad.exe", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl, EntryPoint = "acedCmd")] 347 | public static extern int acedCmd(IntPtr vlist); 348 | 349 | [DllImport("acad.exe", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)] 350 | public static extern int ads_queueexpr(string strExpr); 351 | 352 | [DllImport("acad.exe", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl, EntryPoint = "?acedPostCommand@@YAHPB_W@Z")] 353 | public static extern int acedPostCommand(string strExpr); 354 | } 355 | } 356 | -------------------------------------------------------------------------------- /AutoCADCommands/AutoCADCommands.R18.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {EDA1D1A6-3204-4B82-8D5F-4F59815F072E} 9 | Library 10 | Properties 11 | AutoCADCommands 12 | AutoCADCommands 13 | v3.5 14 | 512 15 | true 16 | 17 | 18 | true 19 | full 20 | false 21 | ..\bin\r18\ 22 | TRACE;DEBUG;R18 23 | prompt 24 | 4 25 | ..\bin\r18\AutoCADCommands.xml 26 | false 27 | AutoCADCommands.ruleset 28 | true 29 | 30 | 31 | pdbonly 32 | true 33 | ..\bin\Release\r18\ 34 | TRACE;R18 35 | prompt 36 | 4 37 | false 38 | AutoCADCommands.ruleset 39 | 40 | 41 | 42 | ..\Reference\AutoCAD\r18\acdbmgd.dll 43 | False 44 | 45 | 46 | ..\Reference\AutoCAD\r18\acmgd.dll 47 | False 48 | 49 | 50 | ..\Reference\AutoCAD\r18\AdWindows.dll 51 | False 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | DictionaryViewer.xaml 72 | 73 | 74 | 75 | InputBox.xaml 76 | 77 | 78 | TaskProgressWindow.xaml 79 | 80 | 81 | 82 | 83 | 84 | MultiInputs.xaml 85 | 86 | 87 | 88 | 89 | 90 | 91 | TextReport.xaml 92 | 93 | 94 | 95 | 96 | 97 | Designer 98 | MSBuild:Compile 99 | 100 | 101 | Designer 102 | MSBuild:Compile 103 | 104 | 105 | Designer 106 | MSBuild:Compile 107 | 108 | 109 | MSBuild:Compile 110 | Designer 111 | 112 | 113 | Designer 114 | MSBuild:Compile 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 129 | -------------------------------------------------------------------------------- /AutoCADCommands/AutoCADCommands.R19.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {77B9F8FE-F75B-48C8-8249-6859E8D347E1} 9 | Library 10 | Properties 11 | AutoCADCommands 12 | AutoCADCommands 13 | v4.5 14 | 512 15 | true 16 | 17 | 18 | true 19 | full 20 | false 21 | ..\bin\r19\ 22 | TRACE;DEBUG;R19 23 | prompt 24 | 4 25 | ..\bin\r19\AutoCADCommands.xml 26 | false 27 | AutoCADCommands.ruleset 28 | true 29 | 30 | 31 | pdbonly 32 | true 33 | ..\bin\Release\r19\ 34 | TRACE;R19 35 | prompt 36 | 4 37 | false 38 | AutoCADCommands.ruleset 39 | 40 | 41 | 42 | ..\Reference\AutoCAD\r19\accoremgd.dll 43 | False 44 | 45 | 46 | ..\Reference\AutoCAD\r19\acdbmgd.dll 47 | False 48 | 49 | 50 | ..\Reference\AutoCAD\r19\acmgd.dll 51 | False 52 | 53 | 54 | ..\Reference\AutoCAD\r19\AdWindows.dll 55 | False 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | DictionaryViewer.xaml 77 | 78 | 79 | 80 | InputBox.xaml 81 | 82 | 83 | TaskProgressWindow.xaml 84 | 85 | 86 | 87 | 88 | 89 | MultiInputs.xaml 90 | 91 | 92 | 93 | 94 | 95 | 96 | TextReport.xaml 97 | 98 | 99 | 100 | 101 | Designer 102 | MSBuild:Compile 103 | 104 | 105 | Designer 106 | MSBuild:Compile 107 | 108 | 109 | Designer 110 | MSBuild:Compile 111 | 112 | 113 | MSBuild:Compile 114 | Designer 115 | 116 | 117 | Designer 118 | MSBuild:Compile 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 133 | -------------------------------------------------------------------------------- /AutoCADCommands/AutoCADCommands.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {C5F6C326-952B-43E8-9830-311F190BAB11} 9 | Library 10 | Properties 11 | AutoCADCommands 12 | AutoCADCommands 13 | v4.7.1 14 | 512 15 | true 16 | 17 | 18 | true 19 | full 20 | false 21 | ..\bin\r23\ 22 | TRACE;DEBUG;R23 23 | prompt 24 | 4 25 | ..\bin\r23\AutoCADCommands.xml 26 | false 27 | AutoCADCommands.ruleset 28 | true 29 | 30 | 31 | pdbonly 32 | true 33 | ..\bin\Release\r23\ 34 | TRACE;R23 35 | prompt 36 | 4 37 | false 38 | AutoCADCommands.ruleset 39 | 40 | 41 | 42 | ..\packages\AutoCAD.NET.Core.23.0.0\lib\net47\AcCoreMgd.dll 43 | False 44 | 45 | 46 | ..\packages\AutoCAD.NET.23.0.0\lib\net47\AcCui.dll 47 | False 48 | 49 | 50 | ..\packages\AutoCAD.NET.Model.23.0.0\lib\net47\AcDbMgd.dll 51 | False 52 | 53 | 54 | ..\packages\AutoCAD.NET.Model.23.0.0\lib\net47\acdbmgdbrep.dll 55 | False 56 | 57 | 58 | ..\packages\AutoCAD.NET.23.0.0\lib\net47\AcDx.dll 59 | False 60 | 61 | 62 | ..\packages\AutoCAD.NET.23.0.0\lib\net47\AcMgd.dll 63 | False 64 | 65 | 66 | ..\packages\AutoCAD.NET.23.0.0\lib\net47\AcMr.dll 67 | False 68 | 69 | 70 | ..\packages\AutoCAD.NET.23.0.0\lib\net47\AcSeamless.dll 71 | False 72 | 73 | 74 | ..\packages\AutoCAD.NET.23.0.0\lib\net47\AcTcMgd.dll 75 | False 76 | 77 | 78 | ..\packages\AutoCAD.NET.23.0.0\lib\net47\AcWindows.dll 79 | False 80 | 81 | 82 | ..\packages\AutoCAD.NET.23.0.0\lib\net47\AdUIMgd.dll 83 | False 84 | 85 | 86 | ..\packages\AutoCAD.NET.23.0.0\lib\net47\AdUiPalettes.dll 87 | False 88 | 89 | 90 | ..\packages\AutoCAD.NET.23.0.0\lib\net47\AdWindows.dll 91 | False 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | DictionaryViewer.xaml 113 | 114 | 115 | 116 | InputBox.xaml 117 | 118 | 119 | TaskProgressWindow.xaml 120 | 121 | 122 | 123 | 124 | 125 | MultiInputs.xaml 126 | 127 | 128 | 129 | 130 | 131 | 132 | TextReport.xaml 133 | 134 | 135 | 136 | 137 | Designer 138 | MSBuild:Compile 139 | 140 | 141 | Designer 142 | MSBuild:Compile 143 | 144 | 145 | Designer 146 | MSBuild:Compile 147 | 148 | 149 | MSBuild:Compile 150 | Designer 151 | 152 | 153 | Designer 154 | MSBuild:Compile 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 169 | -------------------------------------------------------------------------------- /AutoCADCommands/AutoCADCommands.ruleset: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 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 | 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 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | -------------------------------------------------------------------------------- /AutoCADCommands/DbHelper.cs: -------------------------------------------------------------------------------- 1 | using Autodesk.AutoCAD.DatabaseServices; 2 | using Autodesk.AutoCAD.Geometry; 3 | using Dreambuild.AutoCAD.Internal; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | 8 | namespace Dreambuild.AutoCAD 9 | { 10 | /// 11 | /// Database operation helpers. 12 | /// 13 | public static class DbHelper 14 | { 15 | #region symbol tables & dictionaries 16 | 17 | /// 18 | /// Gets all records of a symbol table. 19 | /// 20 | /// The symbol table ID. 21 | /// The record IDs. 22 | public static ObjectId[] GetSymbolTableRecords(ObjectId symbolTableId) 23 | { 24 | using (var trans = symbolTableId.Database.TransactionManager.StartTransaction()) 25 | { 26 | var table = (SymbolTable)trans.GetObject(symbolTableId, OpenMode.ForRead); 27 | return table.Cast().ToArray(); 28 | } 29 | } 30 | 31 | /// 32 | /// Gets all record names of a symbol table. 33 | /// 34 | /// The symbol table ID. 35 | /// The record names. 36 | public static string[] GetSymbolTableRecordNames(ObjectId symbolTableId) 37 | { 38 | return DbHelper 39 | .GetSymbolTableRecords(symbolTableId) 40 | .QOpenForRead() 41 | .Select(record => record.Name) 42 | .ToArray(); 43 | } 44 | 45 | /// 46 | /// Gets a symbol table record by name. 47 | /// 48 | /// The symbol table ID. 49 | /// The record name. 50 | /// The default value if not found. 51 | /// The factory method if not found. 52 | /// The record ID. 53 | public static ObjectId GetSymbolTableRecord(ObjectId symbolTableId, string name, ObjectId? defaultValue = null, Func create = null) 54 | { 55 | using (var trans = symbolTableId.Database.TransactionManager.StartTransaction()) 56 | { 57 | var table = (SymbolTable)trans.GetObject(symbolTableId, OpenMode.ForRead); 58 | if (table.Has(name)) 59 | { 60 | return table[name]; 61 | } 62 | 63 | if (create != null) 64 | { 65 | var record = create(); 66 | table.UpgradeOpen(); 67 | var result = table.Add(record); 68 | trans.AddNewlyCreatedDBObject(record, true); 69 | trans.Commit(); 70 | return result; 71 | } 72 | } 73 | 74 | return defaultValue.Value; 75 | } 76 | 77 | /// 78 | /// Gets layer ID by name. Creates new if not found. 79 | /// 80 | /// The layer name. 81 | /// The database. 82 | /// The layer ID. 83 | public static ObjectId GetLayerId(string layerName, Database db = null) 84 | { 85 | return DbHelper.GetSymbolTableRecord( 86 | symbolTableId: (db ?? HostApplicationServices.WorkingDatabase).LayerTableId, 87 | name: layerName, 88 | create: () => new LayerTableRecord { Name = layerName }); 89 | } 90 | 91 | /// 92 | /// Gets all layer IDs. 93 | /// 94 | /// The database. 95 | /// The layer IDs. 96 | public static ObjectId[] GetAllLayerIds(Database db = null) 97 | { 98 | return DbHelper.GetSymbolTableRecords((db ?? HostApplicationServices.WorkingDatabase).LayerTableId); 99 | } 100 | 101 | /// 102 | /// Gets all layer names. 103 | /// 104 | /// The database. 105 | /// The layer names. 106 | public static string[] GetAllLayerNames(Database db = null) 107 | { 108 | return DbHelper.GetSymbolTableRecordNames((db ?? HostApplicationServices.WorkingDatabase).LayerTableId); 109 | } 110 | 111 | /// 112 | /// Ensures a layer is visible. 113 | /// 114 | /// The layer name. 115 | public static void EnsureLayerOn(string layerName) 116 | { 117 | var id = DbHelper.GetLayerId(layerName); 118 | id.QOpenForWrite(layer => 119 | { 120 | layer.IsFrozen = false; 121 | layer.IsHidden = false; 122 | layer.IsOff = false; 123 | }); 124 | } 125 | 126 | /// 127 | /// Gets block table record ID by block name. 128 | /// 129 | /// The block name. 130 | /// The database. 131 | /// The block table ID. 132 | public static ObjectId GetBlockId(string blockName, Database db = null) 133 | { 134 | return DbHelper.GetSymbolTableRecord( 135 | symbolTableId: (db ?? HostApplicationServices.WorkingDatabase).BlockTableId, 136 | name: blockName, 137 | defaultValue: ObjectId.Null); 138 | } 139 | 140 | /// 141 | /// Gets all block table record IDs. 142 | /// 143 | /// The database. 144 | /// The object ID array. 145 | public static ObjectId[] GetAllBlockIds(Database db = null) 146 | { 147 | return DbHelper.GetSymbolTableRecords((db ?? HostApplicationServices.WorkingDatabase).BlockTableId); 148 | } 149 | 150 | /// 151 | /// Gets all block names. 152 | /// 153 | /// The database. 154 | /// The block name array. 155 | public static string[] GetAllBlockNames(Database db = null) 156 | { 157 | return DbHelper.GetSymbolTableRecordNames((db ?? HostApplicationServices.WorkingDatabase).BlockTableId); 158 | } 159 | 160 | /// 161 | /// Gets linetype ID by name. Returns the continuous linetype as default if not found. 162 | /// 163 | /// The linetype name. 164 | /// The database. 165 | /// The linetype ID. 166 | public static ObjectId GetLinetypeId(string linetypeName, Database db = null) 167 | { 168 | db = db ?? HostApplicationServices.WorkingDatabase; 169 | return DbHelper.GetSymbolTableRecord( 170 | symbolTableId: db.LinetypeTableId, 171 | name: linetypeName, 172 | defaultValue: db.ContinuousLinetype); 173 | } 174 | 175 | /// 176 | /// Gets text style ID by name. Returns the current TEXTSTYLE as default if not found. 177 | /// 178 | /// The text style name. 179 | /// Whether to create new if not found. 180 | /// The database. 181 | /// The text style ID. 182 | public static ObjectId GetTextStyleId(string textStyleName, bool createIfNotFound = false, Database db = null) 183 | { 184 | db = db ?? HostApplicationServices.WorkingDatabase; 185 | return DbHelper.GetSymbolTableRecord( 186 | symbolTableId: db.TextStyleTableId, 187 | name: textStyleName, 188 | create: () => new TextStyleTableRecord { Name = textStyleName }, 189 | defaultValue: db.Textstyle); 190 | } 191 | 192 | /// 193 | /// Gets dimension style ID by name. Returns the current DIMSTYLE as default if not found. 194 | /// 195 | /// The dimension style name. 196 | /// The database. 197 | /// The dimension style ID. 198 | public static ObjectId GetDimstyleId(string dimStyleName, Database db = null) 199 | { 200 | db = db ?? HostApplicationServices.WorkingDatabase; 201 | return DbHelper.GetSymbolTableRecord( 202 | symbolTableId: db.DimStyleTableId, 203 | name: dimStyleName, 204 | defaultValue: db.Dimstyle); 205 | } 206 | 207 | /// 208 | /// Gets a dictionary object. 209 | /// 210 | /// The dictionary ID. 211 | /// The entry name. 212 | /// The default value if not found. 213 | /// The factory method if not found. 214 | /// The object ID. 215 | public static ObjectId GetDictionaryObject(ObjectId dictionaryId, string name, ObjectId? defaultValue = null, Func create = null) 216 | { 217 | using (var trans = dictionaryId.Database.TransactionManager.StartTransaction()) 218 | { 219 | var dictionary = (DBDictionary)trans.GetObject(dictionaryId, OpenMode.ForRead); 220 | if (dictionary.Contains(name)) 221 | { 222 | return dictionary.GetAt(name); 223 | } 224 | 225 | if (create != null) 226 | { 227 | var dictObject = create(); 228 | dictionary.UpgradeOpen(); 229 | var result = dictionary.SetAt(name, dictObject); 230 | trans.AddNewlyCreatedDBObject(dictObject, true); 231 | trans.Commit(); 232 | return result; 233 | } 234 | } 235 | 236 | return defaultValue.Value; 237 | } 238 | 239 | /// 240 | /// Gets group ID by name. 241 | /// 242 | /// The group name. 243 | /// The database. 244 | /// The group ID. 245 | public static ObjectId GetGroupId(string groupName, Database db = null) 246 | { 247 | return DbHelper.GetDictionaryObject( 248 | dictionaryId: (db ?? HostApplicationServices.WorkingDatabase).GroupDictionaryId, 249 | name: groupName, 250 | defaultValue: ObjectId.Null); 251 | } 252 | 253 | /// 254 | /// Gets group ID by entity ID. 255 | /// 256 | /// The entity ID. 257 | /// The group ID. 258 | public static ObjectId GetGroupId(ObjectId entityId) 259 | { 260 | var groupDict = entityId.Database.GroupDictionaryId.QOpenForRead(); 261 | var entity = entityId.QOpenForRead(); 262 | try 263 | { 264 | return groupDict 265 | .Cast() 266 | .First(entry => entry.Value.QOpenForRead().Has(entity)) 267 | .Value; 268 | } 269 | catch 270 | { 271 | return ObjectId.Null; 272 | } 273 | } 274 | 275 | /// 276 | /// Gets all entity IDs in a group. 277 | /// 278 | /// The group ID. 279 | /// The entity IDs. 280 | public static IEnumerable GetEntityIdsInGroup(ObjectId groupId) 281 | { 282 | var group = groupId.QOpenForRead(); 283 | if (group != null) 284 | { 285 | return group.GetAllEntityIds(); 286 | } 287 | 288 | return new ObjectId[0]; 289 | } 290 | 291 | #endregion 292 | 293 | #region xdata 294 | 295 | [Obsolete("Legacy data store mechanism. Use FlexDataStore instead.")] 296 | public static string GetFirstXData(this DBObject dbo, string appName) 297 | { 298 | var xdataForApp = dbo.GetXDataForApplication(appName); 299 | if (xdataForApp == null) 300 | { 301 | return string.Empty; 302 | } 303 | foreach (var value in xdataForApp.AsArray()) 304 | { 305 | if (value.TypeCode != (int)DxfCode.ExtendedDataRegAppName) 306 | { 307 | return value.Value.ToString(); 308 | } 309 | } 310 | return string.Empty; 311 | } 312 | 313 | [Obsolete("Legacy data store mechanism. Use FlexDataStore instead.")] 314 | public static object GetFirstXDataT(this DBObject dbo, string appName) 315 | { 316 | var xdataForApp = dbo.GetXDataForApplication(appName); 317 | if (xdataForApp == null) 318 | { 319 | return null; 320 | } 321 | foreach (var value in xdataForApp.AsArray()) 322 | { 323 | if (value.TypeCode != (int)DxfCode.ExtendedDataRegAppName) 324 | { 325 | return value.Value; 326 | } 327 | } 328 | return null; 329 | } 330 | 331 | [Obsolete("Legacy data store mechanism. Use FlexDataStore instead.")] 332 | public static void SetFirstXData(this DBObject dbo, string appName, string value) 333 | { 334 | dbo.XData = new ResultBuffer(new[] 335 | { 336 | new TypedValue((int)DxfCode.ExtendedDataRegAppName, appName), 337 | new TypedValue((int)DxfCode.ExtendedDataAsciiString, value) 338 | }); 339 | } 340 | 341 | [Obsolete("Legacy data store mechanism. Use FlexDataStore instead.")] 342 | public static void SetFirstXDataT(this DBObject dbo, string appName, object value) // newly 20111207 343 | { 344 | var typeCode = value is Int16 345 | ? (int)DxfCode.ExtendedDataInteger16 346 | : value is Int32 347 | ? (int)DxfCode.ExtendedDataInteger32 348 | : value is Double 349 | ? (int)DxfCode.ExtendedDataReal 350 | : (int)DxfCode.ExtendedDataAsciiString; 351 | 352 | dbo.XData = new ResultBuffer(new[] 353 | { 354 | new TypedValue((int)DxfCode.ExtendedDataRegAppName, appName), 355 | new TypedValue(typeCode, value) 356 | }); 357 | } 358 | 359 | [Obsolete("Legacy data store mechanism. Use FlexDataStore instead.")] 360 | public static string GetFirstXData(this ObjectId dboId, string appName) 361 | { 362 | return dboId.QOpenForRead().GetFirstXData(appName); 363 | } 364 | 365 | [Obsolete("Legacy data store mechanism. Use FlexDataStore instead.")] 366 | public static void SetFirstXData(this ObjectId dboId, string appName, string value) 367 | { 368 | dboId.QOpenForWrite(dbo => dbo.SetFirstXData(appName, value)); 369 | } 370 | 371 | [Obsolete("Legacy data store mechanism. Use FlexDataStore instead.")] 372 | public static void SetFirstXDataT(this ObjectId dboId, string appName, object value) // newly 20111207 373 | { 374 | dboId.QOpenForWrite(dbo => dbo.SetFirstXDataT(appName, value)); 375 | } 376 | 377 | /// 378 | /// Makes sure app names are registered. 379 | /// 380 | /// The app names. 381 | [Obsolete("Legacy data store mechanism. Use FlexDataStore instead.")] 382 | public static void AffirmRegApp(params string[] appNames) // newly 20130122 383 | { 384 | DbHelper.AffirmRegApp(HostApplicationServices.WorkingDatabase, appNames); 385 | } 386 | 387 | /// 388 | /// Makes sure app names are registered. 389 | /// 390 | /// The database to register to. 391 | /// The app names. 392 | [Obsolete("Legacy data store mechanism. Use FlexDataStore instead.")] 393 | public static void AffirmRegApp(Database db, params string[] appNames) // newly 20130122 394 | { 395 | using (var trans = db.TransactionManager.StartTransaction()) 396 | { 397 | var table = trans.GetObject(db.RegAppTableId, OpenMode.ForRead) as RegAppTable; 398 | foreach (string appName in appNames) 399 | { 400 | if (!table.Has(appName)) 401 | { 402 | table.UpgradeOpen(); 403 | var record = new RegAppTableRecord 404 | { 405 | Name = appName 406 | }; 407 | table.Add(record); 408 | trans.AddNewlyCreatedDBObject(record, true); 409 | } 410 | } 411 | trans.Commit(); 412 | } 413 | } 414 | 415 | #endregion 416 | 417 | #region code 418 | 419 | [Obsolete("Legacy data store mechanism. Use FlexDataStore instead.")] 420 | public static void SetCode(this Entity entity, string code) 421 | { 422 | entity.XData = new ResultBuffer(new[] 423 | { 424 | new TypedValue((int)DxfCode.ExtendedDataRegAppName, Consts.AppNameForCode), 425 | new TypedValue((int)DxfCode.ExtendedDataAsciiString, code) 426 | }); 427 | } 428 | 429 | [Obsolete("Legacy data store mechanism. Use FlexDataStore instead.")] 430 | public static void SetCode(this ObjectId entityId, string code) 431 | { 432 | using (var trans = entityId.Database.TransactionManager.StartTransaction()) 433 | { 434 | var entity = (Entity)trans.GetObject(entityId, OpenMode.ForWrite); 435 | DbHelper.SetCode(entity, code); 436 | trans.Commit(); 437 | } 438 | } 439 | 440 | [Obsolete("Legacy data store mechanism. Use FlexDataStore instead.")] 441 | public static string GetCode(this Entity entity) 442 | { 443 | var resBuf = entity.GetXDataForApplication(Consts.AppNameForCode); 444 | if (resBuf == null) 445 | { 446 | return null; 447 | } 448 | foreach (var tValue in resBuf.AsArray()) 449 | { 450 | if (tValue.TypeCode == (int)DxfCode.ExtendedDataAsciiString) 451 | { 452 | return tValue.Value.ToString(); 453 | } 454 | } 455 | return null; 456 | } 457 | 458 | [Obsolete("Legacy data store mechanism. Use FlexDataStore instead.")] 459 | public static string GetCode(this ObjectId entityId) 460 | { 461 | using (var trans = entityId.Database.TransactionManager.StartTransaction()) 462 | { 463 | var entity = (Entity)trans.GetObject(entityId, OpenMode.ForRead); 464 | return DbHelper.GetCode(entity); 465 | } 466 | } 467 | 468 | #endregion 469 | 470 | #region block attribute 471 | 472 | /// 473 | /// Returns a dictionary of a block reference's block attribute names and ObjectIds. 474 | /// 475 | /// The block reference. 476 | /// The result. 477 | public static Dictionary GetBlockAttributeIds(this BlockReference blockReference) 478 | { 479 | var attrs = new Dictionary(); 480 | foreach (ObjectId attrId in blockReference.AttributeCollection) 481 | { 482 | // if block reference is already write enabled, trying to OpenForRead will throw. 483 | if (blockReference.IsWriteEnabled) 484 | { 485 | attrId.QOpenForWrite(attr => 486 | { 487 | attrs.Add(attr.Tag, attrId); 488 | }); 489 | } 490 | else 491 | { 492 | var attr = attrId.QOpenForRead(); 493 | attrs.Add(attr.Tag, attrId); 494 | 495 | } 496 | } 497 | return attrs; 498 | } 499 | 500 | /// 501 | /// Gets block attributes. 502 | /// 503 | /// The block reference. 504 | /// The result. 505 | public static Dictionary GetBlockAttributes(this BlockReference blockReference) 506 | { 507 | var attrs = new Dictionary(); 508 | foreach (ObjectId attrId in blockReference.AttributeCollection) 509 | { 510 | // if block reference is already write enabled, trying to OpenForRead will throw. 511 | if (blockReference.IsWriteEnabled) 512 | { 513 | attrId.QOpenForWrite(attr => 514 | { 515 | attrs.Add(attr.Tag, attr.TextString); 516 | }); 517 | } 518 | else 519 | { 520 | var attr = attrId.QOpenForRead(); 521 | attrs.Add(attr.Tag, attr.TextString); 522 | } 523 | } 524 | return attrs; 525 | } 526 | 527 | /// 528 | /// Get block attribute. 529 | /// 530 | /// The block reference. 531 | /// The tag. 532 | /// The value. 533 | public static string GetBlockAttribute(this BlockReference blockReference, string tag) 534 | { 535 | var attrs = DbHelper.GetBlockAttributes(blockReference); 536 | if (attrs.ContainsKey(tag)) 537 | { 538 | return attrs[tag]; 539 | } 540 | 541 | return null; 542 | } 543 | 544 | /// 545 | /// Defines a block attribute. 546 | /// 547 | /// The block name. 548 | /// The tag. 549 | /// The value. 550 | /// The prompt. 551 | /// The position. 552 | /// The style. 553 | public static void DefineBlockAttribute(string blockName, string tag, string value, string prompt, Point3d position, ObjectId style) 554 | { 555 | var ad = new AttributeDefinition(position, value, tag, prompt, style); 556 | using (var trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction()) 557 | { 558 | var block = trans.GetObject(DbHelper.GetBlockId(blockName), OpenMode.ForWrite) as BlockTableRecord; 559 | block.AppendEntity(ad); 560 | trans.AddNewlyCreatedDBObject(ad, true); 561 | trans.Commit(); 562 | } 563 | } 564 | 565 | /// 566 | /// Appends an attribute to a block reference. This extends the native AppendAttribute Method. 567 | /// 568 | /// The block reference. 569 | /// The attribute definition. 570 | /// Overwrite if the attribute already exists. 571 | /// Create the attribute if it doesn't already exist. 572 | public static void AppendAttribute(this BlockReference blockReference, AttributeReference attributeReference, bool overwrite = true, bool createIfMissing = true) 573 | { 574 | using (var trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction()) 575 | { 576 | var attrs = blockReference.GetBlockAttributes(); 577 | if (!attrs.ContainsKey(attributeReference.Tag)) 578 | { 579 | if (createIfMissing) 580 | { 581 | blockReference.AttributeCollection.AppendAttribute(attributeReference); 582 | } 583 | } 584 | else 585 | { 586 | if (overwrite) 587 | { 588 | blockReference.AttributeCollection.AppendAttribute(attributeReference); 589 | } 590 | } 591 | trans.Commit(); 592 | } 593 | } 594 | 595 | #endregion 596 | 597 | #region ezdata 598 | 599 | public static string GetData(this ObjectId id, string dict, string key) 600 | { 601 | return CustomObjectDictionary.GetValue(id, dict, key); 602 | } 603 | 604 | public static void SetData(this ObjectId id, string dict, string key, string value) 605 | { 606 | CustomObjectDictionary.SetValue(id, dict, key, value); 607 | } 608 | 609 | #endregion 610 | 611 | #region tags 612 | 613 | [Obsolete("Legacy data store mechanism. Use FlexDataStore instead.")] 614 | public static bool HasTag(this DBObject dbo, string tag) 615 | { 616 | var buffer = dbo.GetXDataForApplication(Consts.AppNameForTags); 617 | return buffer.AsArray().Any(typedValue => typedValue.TypeCode == (int)DxfCode.ExtendedDataAsciiString 618 | && typedValue.Value == tag); 619 | } 620 | 621 | [Obsolete("Legacy data store mechanism. Use FlexDataStore instead.")] 622 | public static bool HasTag(this ObjectId id, string tag) 623 | { 624 | using (var trans = id.Database.TransactionManager.StartTransaction()) 625 | { 626 | var dbo = (DBObject)trans.GetObject(id, OpenMode.ForWrite); 627 | return DbHelper.HasTag(dbo, tag); 628 | } 629 | } 630 | 631 | [Obsolete("Legacy data store mechanism. Use FlexDataStore instead.")] 632 | public static void AddTag(this DBObject dbo, string tag) 633 | { 634 | var buffer = dbo.GetXDataForApplication(Consts.AppNameForTags); 635 | buffer.Add(new TypedValue((int)DxfCode.ExtendedDataAsciiString, tag)); 636 | dbo.XData = buffer; 637 | } 638 | 639 | [Obsolete("Legacy data store mechanism. Use FlexDataStore instead.")] 640 | public static void AddTag(this ObjectId id, string tag) 641 | { 642 | using (var trans = id.Database.TransactionManager.StartTransaction()) 643 | { 644 | var dbo = (DBObject)trans.GetObject(id, OpenMode.ForWrite); 645 | DbHelper.AddTag(dbo, tag); 646 | trans.Commit(); 647 | } 648 | } 649 | 650 | [Obsolete("Legacy data store mechanism. Use FlexDataStore instead.")] 651 | public static void RemoveTag(this DBObject dbo, string tag) 652 | { 653 | var buffer = dbo.GetXDataForApplication(Consts.AppNameForTags); 654 | var data = buffer.AsArray().Where(typedValue => typedValue.TypeCode == (int)DxfCode.ExtendedDataAsciiString 655 | && typedValue.Value != tag).ToArray(); 656 | dbo.XData = new ResultBuffer(data); 657 | } 658 | 659 | [Obsolete("Legacy data store mechanism. Use FlexDataStore instead.")] 660 | public static void RemoveTag(this ObjectId id, string tag) 661 | { 662 | using (var trans = id.Database.TransactionManager.StartTransaction()) 663 | { 664 | var dbo = (DBObject)trans.GetObject(id, OpenMode.ForWrite); 665 | DbHelper.RemoveTag(dbo, tag); 666 | trans.Commit(); 667 | } 668 | } 669 | 670 | #endregion 671 | 672 | /// 673 | /// Initializes the database. 674 | /// 675 | /// 676 | /// Call this at the launch of your app and each time you create new doc. 677 | /// 678 | [Obsolete("Legacy data store mechanism. Use FlexDataStore instead.")] 679 | public static void InitializeDatabase(Database db = null) 680 | { 681 | DbHelper.AffirmRegApp(db ?? HostApplicationServices.WorkingDatabase, new[] 682 | { 683 | Consts.AppNameForCode, 684 | Consts.AppNameForID, 685 | Consts.AppNameForName, 686 | Consts.AppNameForTags 687 | }); 688 | } 689 | 690 | internal static Database GetDatabase(IEnumerable objectIds) 691 | { 692 | return objectIds.Select(id => id.Database).Distinct().Single(); 693 | } 694 | } 695 | } 696 | -------------------------------------------------------------------------------- /AutoCADCommands/FlexDataStore.cs: -------------------------------------------------------------------------------- 1 | using Autodesk.AutoCAD.DatabaseServices; 2 | using System.Linq; 3 | 4 | namespace Dreambuild.AutoCAD 5 | { 6 | /// 7 | /// Flexible data store. FDS is our v3 data store mechanism. Old ways FXD (v1) and CD (v2) should be deprecated. 8 | /// 9 | public class FlexDataStore 10 | { 11 | private ObjectId DictionaryId { get; } 12 | 13 | internal FlexDataStore(ObjectId dictionaryId) 14 | { 15 | this.DictionaryId = dictionaryId; 16 | } 17 | 18 | /// 19 | /// Gets a value. 20 | /// 21 | /// The key. 22 | /// The value. 23 | public string GetValue(string key) 24 | { 25 | var dictionary = this.DictionaryId.QOpenForRead(); 26 | if (dictionary.Contains(key)) 27 | { 28 | var record = dictionary.GetAt(key).QOpenForRead(); 29 | return record.Data.AsArray().First().Value.ToString(); 30 | } 31 | 32 | return null; 33 | } 34 | 35 | /// 36 | /// Sets a value. 37 | /// 38 | /// The key. 39 | /// The value. 40 | /// The flex data store. 41 | public FlexDataStore SetValue(string key, string value) 42 | { 43 | using (var trans = this.DictionaryId.Database.TransactionManager.StartTransaction()) 44 | { 45 | var dictionary = trans.GetObject(this.DictionaryId, OpenMode.ForWrite) as DBDictionary; 46 | if (dictionary.Contains(key)) 47 | { 48 | trans.GetObject(dictionary.GetAt(key), OpenMode.ForWrite).Erase(); 49 | } 50 | 51 | var record = new Xrecord 52 | { 53 | Data = new ResultBuffer(new TypedValue((int)DxfCode.ExtendedDataAsciiString, value)) 54 | }; 55 | 56 | dictionary.SetAt(key, record); 57 | trans.AddNewlyCreatedDBObject(record, true); 58 | trans.Commit(); 59 | } 60 | 61 | return this; 62 | } 63 | } 64 | 65 | /// 66 | /// The flex data store extensions. 67 | /// 68 | public static class FlexDataStoreExtensions 69 | { 70 | internal const string DwgGlobalStoreName = "FlexDataStore"; 71 | 72 | /// 73 | /// Gets the DWG global flex data store. 74 | /// 75 | /// The database. 76 | /// The flex data store. 77 | public static FlexDataStore FlexDataStore(this Database db) 78 | { 79 | using (var trans = db.TransactionManager.StartTransaction()) 80 | { 81 | var namedObjectsDict = trans.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead) as DBDictionary; 82 | if (!namedObjectsDict.Contains(FlexDataStoreExtensions.DwgGlobalStoreName)) 83 | { 84 | namedObjectsDict.UpgradeOpen(); 85 | var dwgGlobalStore = new DBDictionary(); 86 | var storeId = namedObjectsDict.SetAt(FlexDataStoreExtensions.DwgGlobalStoreName, dwgGlobalStore); 87 | trans.AddNewlyCreatedDBObject(dwgGlobalStore, true); 88 | trans.Commit(); 89 | return new FlexDataStore(storeId); 90 | } 91 | 92 | trans.Abort(); 93 | return new FlexDataStore(namedObjectsDict.GetAt(FlexDataStoreExtensions.DwgGlobalStoreName)); 94 | } 95 | } 96 | 97 | /// 98 | /// Gets an object's flex data store. 99 | /// 100 | /// The object ID. 101 | /// The flex data store. 102 | public static FlexDataStore FlexDataStore(this ObjectId id) 103 | { 104 | using (var trans = id.Database.TransactionManager.StartTransaction()) 105 | { 106 | var dbo = trans.GetObject(id, OpenMode.ForRead); 107 | if (dbo.ExtensionDictionary == ObjectId.Null) 108 | { 109 | dbo.UpgradeOpen(); 110 | dbo.CreateExtensionDictionary(); 111 | trans.Commit(); 112 | return new FlexDataStore(dbo.ExtensionDictionary); 113 | } 114 | 115 | trans.Abort(); 116 | return new FlexDataStore(dbo.ExtensionDictionary); 117 | } 118 | } 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /AutoCADCommands/GUIs/DictionaryViewer.xaml: -------------------------------------------------------------------------------- 1 |  7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /AutoCADCommands/GUIs/DictionaryViewer.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Windows; 6 | using System.Windows.Controls; 7 | using System.Windows.Data; 8 | using System.Windows.Documents; 9 | using System.Windows.Input; 10 | using System.Windows.Media; 11 | using System.Windows.Media.Imaging; 12 | using System.Windows.Navigation; 13 | using System.Windows.Shapes; 14 | 15 | namespace Dreambuild.AutoCAD 16 | { 17 | /// 18 | /// DictionaryViewer.xaml code behind. 19 | /// 20 | public partial class DictionaryViewer : Window 21 | { 22 | private readonly Func> _getDictNames; 23 | private readonly Func> _getEntryNames; 24 | private readonly Func _getValue; 25 | private readonly Action _setValue; 26 | 27 | public DictionaryViewer() 28 | { 29 | InitializeComponent(); 30 | } 31 | 32 | public DictionaryViewer( 33 | Func> getDictNames, 34 | Func> getEntryNames, 35 | Func getValue, 36 | Action setValue) 37 | { 38 | InitializeComponent(); 39 | 40 | _getDictNames = getDictNames; 41 | _getEntryNames = getEntryNames; 42 | _getValue = getValue; 43 | _setValue = setValue; 44 | } 45 | 46 | public void DictionaryViewer_Loaded(object sender, RoutedEventArgs e) 47 | { 48 | _getDictNames().ForEach(name => this.DictionaryList.Items.Add(name)); 49 | this.DictionaryList.SelectedIndex = 0; 50 | } 51 | 52 | private void DictionaryList_SelectionChanged(object sender, SelectionChangedEventArgs e) 53 | { 54 | this.EntryList.Items.Clear(); 55 | string dict = this.DictionaryList.SelectedItem.ToString(); 56 | _getEntryNames(dict) 57 | .OrderBy(name => name) 58 | .Select(name => new 59 | { 60 | Key = name, 61 | Value = _getValue(dict, name) 62 | }) 63 | .ForEach(entry => this.EntryList.Items.Add(entry)); 64 | } 65 | 66 | private void EntryList_MouseDoubleClick(object sender, MouseButtonEventArgs e) 67 | { 68 | if (this.DictionaryList.SelectedIndex == -1 || this.EntryList.SelectedIndex == -1) 69 | { 70 | return; 71 | } 72 | 73 | string dict = this.DictionaryList.SelectedItem.ToString(); 74 | string key = _getEntryNames(dict).OrderBy(name => name).ToList()[this.EntryList.SelectedIndex]; 75 | string oldValue = _getValue(dict, key); 76 | 77 | var inputBox = new InputBox(oldValue) 78 | { 79 | Owner = this 80 | }; 81 | 82 | if (inputBox.ShowDialog() == true) 83 | { 84 | _setValue(dict, key, inputBox.Value); 85 | } 86 | 87 | // Update ListView 88 | this.DictionaryList_SelectionChanged(null, null); 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /AutoCADCommands/GUIs/InputBox.xaml: -------------------------------------------------------------------------------- 1 |  6 | 7 | 8 |