Clone Method & Handle

Discuss and ask questions about CAD .NET library.

Moderators: SDS, support, admin

Post Reply
Sing
Posts: 48
Joined: 12 Apr 2020, 17:39

Clone Method & Handle

Post by Sing » 28 Aug 2020, 07:03

Hello.

First,
I usually use two CAD images (one is a template, the other is loaded from a source file to edit).
when I clone a entity from a template CAD image, the handle is the same as the one cloned in the template CAD image.
And then, I add the cloned entity to a source CAD image.
I think... if the handle value is already contained in a source CAD image, it will conflict with each other when opening output.dwg file converted from the source CAD image.

Can I manage the handles of entities cloned? Reordering or Editing?

Second,
I cloned Color and CADHatch and add them to an empty CAD image successfully.
CADDimension, however, doesn't work. All Code is working clearly, but the output.dwg is not opened.
The error message 'Drawing file is not valid' is poped up on CADViewer (Autodesk DWG TrueView 2018).

The code is below.

Code: Select all

string dwgFile = "dimention.dwg";	// <-- Template dwg file

CADImage cadImage = null;
cadImage = CADImage.CreateImageByExtension(dwgFile);
cadImage.LoadFromFile(dwgFile);

CADImage cadImage2 = new CADImage();
cadImage2.InitialNewImage();

//CADLayer vlayer = cadImage2.Converter.LayerByName("tempLayer");

CADDimension dimensionL = new CADDimension();

foreach (var entity in cadImage.Converter.Entities)
{
	if (entity.GetType() == typeof(CADDimension))
	{
		dimensionL.Clone(entity);
		break;
	}
}

//dimensionL.LinDefPoint1 = new DPoint(100, 100, 0);
//dimensionL.LinDefPoint2 = new DPoint(100, 1000, 0);
//dimensionL.DefPoint = new DPoint(100, 500, 0);
//dimensionL.TextPosVert = DimTextPosVert.Center;
//dimensionL.TextAlign = 0;
////dimensionL.Style = vDimStyle;
//dimensionL.Layer = vlayer;
//dimensionL.Color = System.Drawing.Color.White;

cadImage2.Converter.Loads(dimensionL);
cadImage2.Layouts[0].AddEntity(dimensionL);				
//cadImage2.GetExtents();

CADImport.Export.CADtoDWG.SaveAsDWG(cadImage2, "output.dwg");

if (cadImage != null)
{
	cadImage.ClearBuffer();
	cadImage.Dispose();
	cadImage = null;
}

if (cadImage2 != null)
{
	cadImage2.ClearBuffer();
	cadImage2.Dispose();
	cadImage2 = null;
}

GC.Collect();
Thank you.
Attachments
dimension.zip
(22.08 KiB) Downloaded 455 times

support
Posts: 3254
Joined: 30 Mar 2005, 11:36
Contact:

Re: Clone Method & Handle

Post by support » 28 Aug 2020, 23:50

Hello,

The output DWG file may be corrupted because of duplicate entity handles. AutoCAD or DWG TrueView normally cannot read the drawing file containing duplicate entity handles and gives an error. To avoid this problem, you should use a CADEntity.AssignEntity method which copies properties of the source entity passed as a parameter to the CADEntity object, but doesn't keep the original handle value.

CADDimension object refers to a dimension block with the name *Dxxx, where xxx is a number. The given dimension block must be cloned as well as the dimension style and the dimension text style.

Below you will find a code for DeepCloneObject and CloneStyle methods which demonstrate how to clone different CAD objects and styles.

Code: Select all

public static void CloneStyle(CADImage targetImage, FaceModule.ConvSection styleSection, CADEntity srcStyle)
{
    if (srcStyle.EntName == "Standard")
    {
        if (srcStyle.GetType() == typeof(CADDimensionStyle))
        {
            CADDimensionStyle dimStyle = targetImage.Converter.DimensionStyleByName("Standard");
            dimStyle.AssignEntity(srcStyle);
            targetImage.Converter.Loads(dimStyle);
        }

        if (srcStyle.GetType() == typeof(CADStyle))
        {
            CADStyle style = targetImage.Converter.StyleByName("Standard");
            style.AssignEntity(srcStyle);
            targetImage.Converter.Loads(style);
        }
    }
    else
    {
        DeepCloneObject(targetImage, styleSection, srcStyle);
    }
}

public static void DeepCloneObject(CADImage targetImage, FaceModule.ConvSection targetSection, CADEntity source)
{
    CADEntity clone = (CADEntity)Activator.CreateInstance(source.GetType());
    clone.AssignEntity(source);
    clone.ExtendedData.AddRange(source.ExtendedData);
    targetImage.Converter.Loads(clone);

    if (targetSection == FaceModule.ConvSection.Entities)
    {
        // Clone the source entity layer
        DeepCloneObject(targetImage, FaceModule.ConvSection.Layers, source.Layer);
        // Add the cloned entity onto a layout
        targetImage.CurrentLayout.AddEntity(clone);
        targetImage.GetExtents();

        if (source.EntType == EntityType.Dimension)
        {
            CADDimension sourceDim = source as CADDimension;
            // Clone the dimension block
            DeepCloneObject(targetImage, FaceModule.ConvSection.Blocks, sourceDim.Block);
            // Clone the dimension style
            CloneStyle(targetImage, FaceModule.ConvSection.DimStyles, sourceDim.Style);
            // Clone the dimension text style
            CloneStyle(targetImage, FaceModule.ConvSection.Styles, sourceDim.TextStyle);
        }

        if ((source.EntType == EntityType.Text) || (source.EntType == EntityType.MText))
            // Clone the text style
            CloneStyle(targetImage, FaceModule.ConvSection.Styles, (source as CADText).Style);
    }
    else
    {
        if (source.GetType() == typeof(CADBlock))
        {
            // Assign the source block name to the clone
            (clone as CADBlock).Name = (source as CADBlock).Name;                    
        }
        // Add the cloned non-visible object to a corresponding section of the target drawing
        targetImage.Converter.GetSection(targetSection).AddEntity(clone);
    }
}
Mikhail
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

Sing
Posts: 48
Joined: 12 Apr 2020, 17:39

Re: Clone Method & Handle

Post by Sing » 01 Sep 2020, 15:19

Awesome!!
Thank you so much Mikhail.
The code is perfect!

Post Reply