Previously hosted on CodePlex.
AutoCAD Code Pack is a powerful library that helps you to develop AutoCAD plugins using the AutoCAD .NET API. It re-encapsulates the over-designed and old-fashioned classes and methods into easy-to-use static modules and functions. It also brings modern C# syntax like LINQ and lambdas (functional programming) to AutoCAD development. With all the features it provides, you can save over half the lines of your code.
The library was originally developed for AutoCAD R18 (2010, 2011, 2012) and .NET 3.5. We recently updated it to target AutoCAD R23 (2019) and .NET 4.7.1, thanks to the contribution from @lavantgarde. Due to the popularity of old AutoCAD versions, we also provide compatibility projects for R18 and R19.
View Test.cs for API usage examples.
Don't forget to star us! If you think this library is helpful, please tell others to have a try too. We can't wait to hear all your feedbacks.
The library consists of the following modules:
Drawto directly draw entities (with AutoCAD-command-like functions)NoDrawto create in-memory entitiesModifyto edit entities (with AutoCAD-command-like functions)Annotationto draw annotationsDbHelperto manipulate the DWG databaseQuickSelectionto simplify entity manipulation with LINQ style coding experience (like jQuery, if you know some Web)Interactionto handle user interactionsAlgorithmsto offer some mathematical helpersMultiDocto process cross-document scenariosCustomDictionaryto help you attach data to entitiesSymbolPackto help you draw symbols like arrows, etc.IronPythonto allow you use IronPython in AutoCAD
You may write this elegant code with the code pack. Let's say you want a command to clean up all 0-length polylines.
[CommandMethod("PolyClean0",CommandFlags.UsePickSet)]publicstaticvoidPolyClean0(){varids=Interaction.GetSelection("\nSelect polyline","LWPOLYLINE");intn=0;ids.QForEach<Polyline>(poly =>{if(poly.Length==0){poly.Erase();n++;}});Interaction.WriteLine("{0} eliminated.",n);}Crazy simple, right? Can you imagine how much extra code you would have to write using the original API:
[CommandMethod("PolyClean0_Old",CommandFlags.UsePickSet)]publicstaticvoidPolyClean0_Old(){stringmessage="\nSelect polyline";stringallowedType="LWPOLYLINE";Editored=Application.DocumentManager.MdiActiveDocument.Editor;PromptSelectionOptionsopt=newPromptSelectionOptions{MessageForAdding=message};ed.WriteMessage(message);SelectionFilterfilter=newSelectionFilter(newTypedValue[]{newTypedValue(0,allowedType)});PromptSelectionResultres=ed.GetSelection(opt,filter);if(res.Status!=PromptStatus.OK){return;}ObjectId[]ids=res.Value.GetObjectIds();intn=0;Databasedb=HostApplicationServices.WorkingDatabase;using(Transactiontrans=db.TransactionManager.StartTransaction()){foreach(ObjectIdidinids){Polylinepoly=trans.GetObject(id,OpenMode.ForWrite)asPolyline;if(poly.Length==0){poly.Erase();n++;}}trans.Commit();}ed.WriteMessage("{0} eliminated.",n);}View Test.cs for detailed API usage examples.
SharpDiskSweeper can help you find the point in disk to start cleaning up.