Adding CadInsertObject in a DWG created from scratch

Hi Aspose Support,

I’m working with the latest version of Aspose.CAD for .NET, and I’m building a tool where I need to create a DWG file completely from scratch.

My goal is to add several points to the drawing, but these points need to be visualized using symbolic representations (e.g., a small circle or cross), using blocks.

What I want to do:
Define a block symbol using CadBlockEntity (e.g., a simple circle centered at 0,0).

Register this block properly within the DWG structure (using BlockEntities and/or block tables, if required).

Insert multiple instances of this block using CadInsertObject, positioning them using 3D coordinates (Cad3DPoint).

Add those block references to the drawing, so they are actually visible in tools like AutoCAD or DWG viewers.

Important:
This is not based on an existing DWG template. The drawing is created from a blank new DwgImage() instance.

Request:
Could you please provide a minimal working example that shows how to:

Build and register a block definition.

Insert it correctly using CadInsertObject.

Add those entities to a brand new DWG file.

Ensure the blocks display visually as expected.

I’m looking for a clean, working example that includes any required steps like BasePoint, layer assignments, and how to make sure the inserted blocks are rendered properly in the final drawing.

Thanks in advance for your support — I’d really appreciate a step-by-step or code sample that demonstrates this process clearly.

Best regards,
Abdel

@Abne

To create a DWG file from scratch and add symbolic representations using blocks in Aspose.CAD for .NET, you can follow the steps outlined below. This example demonstrates how to define a block, register it, and insert it into a new DWG file.

Step-by-Step Code Example

  1. Create a new DWG file.
  2. Define a block symbol using CadBlockEntity.
  3. Register the block in the DWG structure.
  4. Insert the block using CadInsertObject at specified 3D coordinates.

Here’s a minimal working example:

using Aspose.CAD.FileFormats.Cad;
using Aspose.CAD.FileFormats.Cad.CadObjects;
using System;

namespace Aspose.CAD.Examples
{
    class CreateDWGWithBlocks
    {
        public static void Run()
        {
            // Step 1: Create a new DWG image
            CadImage cadImage = new CadImage();

            // Step 2: Define a block symbol (e.g., a simple circle)
            CadBlockEntity circleBlock = new CadBlockEntity("CircleBlock");
            circleBlock.Entities.Add(new CadCircle(new Cad3DPoint(0, 0, 0), 1)); // Circle with radius 1
            cadImage.BlockEntities.Add(circleBlock);

            // Step 3: Insert the block into the model space
            CadInsertObject insertCircle = new CadInsertObject("CircleBlock", new Cad3DPoint(10, 10, 0)); // Position at (10, 10, 0)
            cadImage.BlockEntities["*Model_Space"].AddEntity(insertCircle);

            // Step 4: Save the DWG file
            string outputPath = "output.dwg";
            cadImage.Save(outputPath);
            Console.WriteLine($"DWG file created at: {outputPath}");
        }
    }
}

Explanation of the Code

  • Creating a New DWG Image: The CadImage class is instantiated to create a new DWG file.
  • Defining a Block: A CadBlockEntity is created to represent a circle. The circle is defined with a center point and a radius.
  • Registering the Block: The block is added to the BlockEntities collection of the CadImage.
  • Inserting the Block: A CadInsertObject is created to insert the block into the model space at specified coordinates.
  • Saving the DWG File: Finally, the Save method is called to write the DWG file to disk.

Important Notes

  • Ensure that you have the necessary references to the Aspose.CAD library in your project.
  • You can modify the block definition and insertion points as needed to suit your requirements.

This example should help you get started with creating a DWG file and adding blocks to it using Aspose.CAD for .NET. If you have any further questions or need additional assistance, feel free to ask!

@Abne,
Hi,
we have created CADNET-10126 to prepare example for this case.

@Abne,
Hi,
please find if the attached example is useful.
AddInsert.zip (1012 Bytes)

Hi @oleksii.gorokhovatskyi,
I just want to thank you for sharing the AddInsert.zip example.
It really helped me solve the issue with creating the insert object.

I truly appreciate your support and time!
Abdel.

@Abne,
thank you for kind words, we are happy to help :slight_smile: