ACadSharp Documentation

repository·master·Indexed 21 days ago

https://github.com/domcr/acadsharp

A .NET library for reading, writing, and modifying CAD files in DXF and DWG formats. It provides low-level access to geometric entities and CAD tables such as layers, blocks, and styles. The library supports various versions of DXF (binary and ASCII) and DWG, offering tools like DwgReader, DxfReader, and CadReaderFactory for file I/O and document manipulation.

Tokens
2.4K
Snippets
8
Records
18
Agent score
73%

What's inside ACadSharp

  1. Overview of ACadSharp features

    master

    ACadSharp is a .NET library designed for reading and writing CAD files, specifically supporting DXF and DWG formats. It allows developers to extract or modify geometric information from entities and manage table elements such as Blocks, Layers, and Styles.

    Key Capabilities:

    • DXF Support: Read and write both binary and ASCII DXF files.
    • DWG Support: Read and write DWG files.
    • Entity Manipulation: Extract or modify geometric information from various entities.
    • Table Management: Control, create, or modify Blocks, Layers, and Styles.

    Note: For PDF export capabilities, use the separate ACadSharp.Pdf project.

  2. Explore the ACadSharp Namespaces

    master

    ACadSharp is organized into several namespaces that categorize core CAD functionality. Use these namespaces to locate specific types for document manipulation, geometric entities, and file I/O:

    • ACadSharp: Contains core types like CadDocument, CadObject, and ACadVersion.
    • ACadSharp.Entities: Contains geometric entity types (e.g., lines, arcs, polylines, text, hatches).
    • ACadSharp.Tables: Defines reusable drawing resources like layers, block records, and text styles.
    • ACadSharp.Tables.Collections: Provides typed table collections accessible via CadDocument.
    • ACadSharp.Objects: Contains non-graphical objects like dictionaries, layouts, groups, and image definitions.
    • ACadSharp.Objects.Collections: Provides typed collections for objects like groups and image definitions.
    • ACadSharp.IO: Contains readers and writers for DWG and DXF formats (DwgReader, DwgWriter, DxfReader, DxfWriter).
    • ACadSharp.Header: Provides strongly-typed access to DXF header system variables via CadHeader.
    • ACadSharp.XData: Supports attaching application-specific data to any CadObject using Extended Entity Data (XDATA).
    • ACadSharp.Attributes: Used for mapping .NET properties to DXF group codes.
    • ACadSharp.Classes: Contains the internal DXF class registry (DxfClassCollection).
  3. Read CAD files using CadReaderFactory

    master

    If the file format (DWG or DXF) is unknown at runtime, use CadReaderFactory.CreateReader to automatically select the appropriate reader based on the file extension. Supported extensions are .dwg and .dxf.

    using ACadSharp.IO;
    using (ICadReader reader = CadReaderFactory.CreateReader("path/to/file.dwg")) 
    {
    	CadDocument doc = reader.Read();
    }
  4. Handle reading notifications and warnings

    master

    All readers expose an OnNotification event. You can subscribe to this event to capture and process warnings or informational messages that occur during the file reading process. The event arguments provide the NotificationType and the Message.

    using ACadSharp.IO;
    using (DwgReader reader = new DwgReader("path/to/file.dwg")) 
    { 
    	reader.OnNotification += (sender, args) => { Console.WriteLine($"[{args.NotificationType}] {args.Message}"); };
    	CadDocument doc = reader.Read();
    }
  5. Write DWG files

    master

    To save a CadDocument to a DWG file, use the DwgWriter class. You can subscribe to the OnNotification event to monitor the writing process, for example by using NotificationHelper.LogConsoleNotification.

    // Writing a DWG file
    using (DwgWriter writer = new DwgWriter(file, doc))
    {
        writer.OnNotification += NotificationHelper.LogConsoleNotification;
        writer.Write();
    }
  6. Read DWG and DXF files

    master

    To read a DWG file, use the DwgReader class. To read a DXF file, replace DwgReader with DxfReader. You can pass a notification delegate (such as NotificationHelper.LogConsoleNotification) to the constructor to monitor the reading process via events.

    // Reading a DWG file
    using (DwgReader reader = new DwgReader(file, NotificationHelper.LogConsoleNotification))
    {
        CadDocument doc = reader.Read();
    }
  7. Read a DXF file using DxfReader

    master

    To load a .dxf file into a CadDocument, instantiate a DxfReader with the file path and call the Read() method. It is recommended to wrap the reader in a using block to ensure proper disposal.

    using ACadSharp.IO;
    using (DxfReader reader = new DxfReader("path/to/file.dxf")) 
    {
    	CadDocument doc = reader.Read();
    }
  8. Read a DWG file using DwgReader

    master

    To read a DWG file, use the DwgReader.Read method. This method accepts the file path and an optional notification handler to process messages during the reading process via NotificationEventArgs.

    public static void Main()
    {
    	string path = "sample.dwg";
    	CadDocument doc = DwgReader.Read(path, onNotification);
    }
    
    // Process a notification from the reader
    private static void onNotification(object sender, NotificationEventArgs e)
    {
    	Console.WriteLine(e.Message);
    }
  9. Explore entities in a CAD document

    master

    Once a document is loaded, you can access all drawing entities through the Entities property of the CadDocument object. This collection represents the entities located in the model space.

    // Get all the entities in the model
    public static IEnumerable<Entity> GetAllEntitiesInModel(string file)
    {
        CadDocument doc = DwgReader.Read(file);
    
        // Get the model space where all the drawing entities are
        return doc.Entities;
    }
  10. Identify points in Radius dimensions

    master

    When working with Radius dimensions, the following points define the dimension's geometry:

    • AngleVertex: The vertex point related to the radius.
    • DefinitionPoint: The point used to define the dimension's orientation or scale.
    • InsertionPoint: The location where the dimension text/leader is placed.
  11. Identify points in Angular3pt dimensions

    master

    When working with Angular3pt dimensions, the following points define the dimension's geometry:

    • FirstPoint: The first point of the angle.
    • SecondPoint: The second point of the angle.
    • AngleVertex: The vertex where the angle is measured.
    • DefinitionPoint: The point used to define the dimension's orientation or scale.
    • InsertionPoint: The location where the dimension text/leader is placed.