Adding CadMText to a new DXF document

Generation of DXF|Documentation

// Add new MTEXT
CadMText watermark = new CadMText();
watermark.Text = "Watermark message";
watermark.InitialTextHeight = 40;
watermark.InsertionPoint = new Cad3DPoint(300, 40);
watermark.LayerName = "0";
cadImage.BlockEntities["*Model_Space"].AddEntity(watermark);

if i take this code and then i want to add a new CadMText item, i always get an error stating that there is no Model_Space block entity.

@kevin.kilburn

To add a new CadMText item to a DXF document without encountering the “no Model_Space block entity” error, you need to ensure that the Model_Space block exists in your CAD image. The error typically arises when the block entities are not properly initialized or if the block name is incorrect.

Here’s a sample code snippet that demonstrates how to add a CadMText entity to the Model_Space block:

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

namespace Aspose.CAD.Examples
{
    class AddMTextExample
    {
        public static void Run()
        {
            // Load the CAD image
            using (CadImage cadImage = (CadImage)Image.Load("path_to_your_dxf_file.dxf"))
            {
                // Check if the Model_Space block exists
                if (cadImage.BlockEntities.ContainsKey("*Model_Space"))
                {
                    // Create a new MTEXT entity
                    CadMText mText = new CadMText
                    {
                        Text = "Your watermark message",
                        InitialTextHeight = 40,
                        InsertionPoint = new Cad3DPoint(300, 40),
                        LayerName = "0"
                    };

                    // Add the MTEXT entity to the Model_Space block
                    cadImage.BlockEntities["*Model_Space"].AddEntity(mText);
                }
                else
                {
                    Console.WriteLine("Model_Space block does not exist.");
                }

                // Save the modified CAD image
                cadImage.Save("path_to_save_modified_dxf_file.dxf");
            }
        }
    }
}

Key Points:

  • Ensure that the Model_Space block is present in your CAD file before attempting to add entities to it.
  • The code checks for the existence of the Model_Space block and only proceeds if it is found.
  • Adjust the InsertionPoint and Text properties of the CadMText as needed for your specific use case.

If you continue to experience issues, please verify that your DXF file is correctly formatted and contains the necessary block definitions.

i guess i should have pasted the copy from the example rather than posting the url to the example. i am starting with a NEW dxf file, not loading an existing one. the code below is what i’m looking at. i am drawing the example from the tutorial code, with some modifications to force the entities visible and distinguishable. as you can see, the mtext simply shows a tiny veritcal line.

        //LINE, CIRCLE, ARC, TEXT, POINT, SOLID, 3DFACE and POLYLINE

        //add line
        CadLine line = new(new Cad3DPoint(0, 0, 0), new Cad3DPoint(100, 100, 100))
        {
            LineWeight = 3,
            ColorValue = global::Aspose.CAD.Color.Orange.ToArgb()
        };
        dxfImage.AddEntity(line);

        //add circle
        CadCircle circle = new(new Cad3DPoint(50, 0), 10)
        {
            LineWeight = 3,
            ColorValue = global::Aspose.CAD.Color.Blue.ToArgb()
        };
        dxfImage.AddEntity(circle);

        //add arc
        CadArc arc = new(new Cad3DPoint(100, 0), 10, 45, 135)
        {
            ColorValue = global::Aspose.CAD.Color.OrangeRed.ToArgb(),
            LineWeight = 3,
        };
        dxfImage.AddEntity(arc);


        CadText text = new()
        {
            FirstAlignment = new Cad3DPoint(0, -50, 0),
            TextHeight = 10,
            ColorValue = global::Aspose.CAD.Color.Black.ToArgb(),
            DefaultValue = "text value"
        };
        dxfImage.AddEntity(text);


        CadPoint point = new()
        {
            CenterPoint = new Cad3DPoint(-10, -10, -10),
            ColorValue = global::Aspose.CAD.Color.Green.ToArgb(),
            LineWeight = 3,
            Radius = 4,
        };
        dxfImage.AddEntity(point);


        CadMText watermark = new()
        {
            ColorValue = global::Aspose.CAD.Color.Red.ToArgb(),
            Text = "Watermark message",
            InitialTextHeight = 10,
            InsertionPoint = new Cad3DPoint(100, 40),
            LayerName = "0"
        };
        //dxfImage.BlockEntities["*Model_Space"].AddEntity(watermark);    < -throws error
        dxfImage.AddEntity(watermark);    // <- end up with a single horizontal line

        using var stream = new MemoryStream();
        dxfImage.UpdateSize();
        dxfImage.Save(stream, new PngOptions
        {
            VectorRasterizationOptions = new CadRasterizationOptions()
            {
                AutomaticLayoutsScaling = true,
                BackgroundColor = Aspose.CAD.Color.White,
                DrawColor = Aspose.CAD.Color.Blue,
                DrawType = CadDrawTypeMode.UseObjectColor,
                PageHeight = 640,
                PageWidth = 640,
                NoScaling = false,
                ScaleMethod = ScaleType.ShrinkToFit
            }
        });

        var png = $"data:image/png;base64,{Convert.ToBase64String(stream.ToArray())}";

image.png (2.2 KB)

@kevin.kilburn,
Hello.
There is no MTEXT entity for DXF R12 file format, which is created by default for DxfImage().
Could you please try if this works:

DxfImage dxfImage = new DxfImage(CadAcadVersion.AC1024);

CadMText watermark = new()
{
     Text = "Watermark message",
     InitialTextHeight = 10,
     InsertionPoint = new Cad3DPoint(0, 0),
};

dxfImage.AddEntity(watermark);

dxfImage.Save(outPath);

That worked. Thank you!

1 Like

@kevin.kilburn.
we are happy to help :slight_smile: