Code Monkey home page Code Monkey logo

autocadcodepack's Introduction

AutoCAD Code Pack

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.

Modules

The library consists of the following modules:

  • Draw to directly draw entities (with AutoCAD-command-like functions)
  • NoDraw to create in-memory entities
  • Modify to edit entities (with AutoCAD-command-like functions)
  • Annotation to draw annotations
  • DbHelper to manipulate the DWG database
  • QuickSelection to simplify entity manipulation with LINQ style coding experience (like jQuery, if you know some Web)
  • Interaction to handle user interactions
  • Algorithms to offer some mathematical helpers
  • MultiDoc to process cross-document scenarios
  • CustomDictionary to help you attach data to entities
  • SymbolPack to help you draw symbols like arrows, etc.
  • IronPython to allow you use IronPython in AutoCAD

A Quick Look

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)]
public static void PolyClean0()
{
    var ids = Interaction.GetSelection("\nSelect polyline", "LWPOLYLINE");
    int n = 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)]
public static void PolyClean0_Old()
{
    string message = "\nSelect polyline";
    string allowedType = "LWPOLYLINE";
    Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
    PromptSelectionOptions opt = new PromptSelectionOptions
    {
        MessageForAdding = message
    };
    ed.WriteMessage(message);
    SelectionFilter filter = new SelectionFilter(
        new TypedValue[] 
        {
            new TypedValue(0, allowedType) 
        });
    PromptSelectionResult res = ed.GetSelection(opt, filter);
    if (res.Status != PromptStatus.OK)
    {
        return;
    }            
    ObjectId[] ids = res.Value.GetObjectIds();
    int n = 0;
    Database db = HostApplicationServices.WorkingDatabase;
    using (Transaction trans = db.TransactionManager.StartTransaction())
    {
        foreach (ObjectId id in ids)
        {
            Polyline poly = trans.GetObject(id, OpenMode.ForWrite) as Polyline;
            if (poly.Length == 0)
            {
                poly.Erase();
                n++;
            }
        }
        trans.Commit();
    }
    ed.WriteMessage("{0} eliminated.", n);
}

Examples

View Test.cs for detailed API usage examples.

PS: A useful tool for those who have a lot of huge DWGs and VS projects on disk

SharpDiskSweeper can help you find the point in disk to start cleaning up.

autocadcodepack's People

Contributors

lavantgarde avatar luanshixia avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.