CADInsert move to multiple layers

Discuss and ask questions about CAD .NET library.

Moderators: SDS, support, admin

Post Reply
SuperCriss123
Posts: 3
Joined: 28 Nov 2023, 15:31

CADInsert move to multiple layers

Post by SuperCriss123 » 28 Nov 2023, 18:16

Hello
I have a GDS File with a CADBlock used in many CADInsert.
I need to color some of these in different colors; so I'm trying to achieve this by moving the CADInsert to new layers and assign diferrent colors to those new layers.
I clone the block and assignt it to the new layer and to the Insert in that layer but it doesn't work

can you Help me?

thank you
Cris

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

Re: CADInsert move to multiple layers

Post by support » 01 Dec 2023, 08:42

SuperCriss123 wrote:
28 Nov 2023, 18:16
Hello
I have a GDS File with a CADBlock used in many CADInsert.
I need to color some of these in different colors; so I'm trying to achieve this by moving the CADInsert to new layers and assign diferrent colors to those new layers.
I clone the block and assignt it to the new layer and to the Insert in that layer but it doesn't work

can you Help me?

thank you
Cris
Hi Cris,
Most likely, you have an insert with another insert (or inserts) inside it. And you place an "outer" insert on a layer, while other inserts (inside this "outer" insert) remain on their own layers.
To say more precisely, could you send me your code example?
Best wishes,
Catherine.
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

SuperCriss123
Posts: 3
Joined: 28 Nov 2023, 15:31

Re: CADInsert move to multiple layers

Post by SuperCriss123 » 01 Dec 2023, 13:01

Hi Catherine,
here a part of the code:
how can I send you the GDS?

Code: Select all

private CADEntity CloneEntity(CADEntity src)
{
    CADEntity result = (CADEntity)Activator.CreateInstance(src.GetType());
    result.AssignEntity(src);
    if (result.EntType == EntityType.Attrib)
        (result as CADAttrib).Value = (src as CADAttrib).Value;
    return result;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    CADLayer layerPart = (CADLayer)cadImage.Converter.GetSection(ConvSection.Layers).Entities
     .FirstOrDefault(s => s.EntName.StartsWith("PartLocationsTest"));

    CADLayer newLayer = GetLayer("errore", Color.Fuchsia);

    //get the first Insert 
    CADInsert ll = (CADInsert)layerPart.Converter.Entities.OrderBy(y => y.Box.bottom).ThenBy(x => x.Box.left).First();

    //clone the first block
    CADBlock blk = (CADBlock)CloneEntity(ll.Block);
    blk.Layer = newLayer;

    //move the first 10 insert to new layer
    for (int i = 0; i < 10; i++)
    {
        CADInsert item = (CADInsert)layerPart.Converter.GetSection(ConvSection.Entities).Entities[i];

        item.Block = blk;
        item.Layer = newLayer;
        newLayer.Converter.Loads(item);
    }

    cadPictBox.Invalidate();            
}

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

Re: CADInsert move to multiple layers

Post by support » 05 Dec 2023, 09:34

SuperCriss123 wrote:
01 Dec 2023, 13:01
Hi Catherine,
here a part of the code:
how can I send you the GDS?

Code: Select all

private CADEntity CloneEntity(CADEntity src)
{
    CADEntity result = (CADEntity)Activator.CreateInstance(src.GetType());
    result.AssignEntity(src);
    if (result.EntType == EntityType.Attrib)
        (result as CADAttrib).Value = (src as CADAttrib).Value;
    return result;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    CADLayer layerPart = (CADLayer)cadImage.Converter.GetSection(ConvSection.Layers).Entities
     .FirstOrDefault(s => s.EntName.StartsWith("PartLocationsTest"));

    CADLayer newLayer = GetLayer("errore", Color.Fuchsia);

    //get the first Insert 
    CADInsert ll = (CADInsert)layerPart.Converter.Entities.OrderBy(y => y.Box.bottom).ThenBy(x => x.Box.left).First();

    //clone the first block
    CADBlock blk = (CADBlock)CloneEntity(ll.Block);
    blk.Layer = newLayer;

    //move the first 10 insert to new layer
    for (int i = 0; i < 10; i++)
    {
        CADInsert item = (CADInsert)layerPart.Converter.GetSection(ConvSection.Entities).Entities[i];

        item.Block = blk;
        item.Layer = newLayer;
        newLayer.Converter.Loads(item);
    }

    cadPictBox.Invalidate();            
}
Hi,
You can send your file at support@cadsofttools.com and briefly describe your problem.
Please, have a look at the code below:

Code: Select all

private void button11_Click(object sender, EventArgs e)
{
    if (cadImage == null) return;
    var ins = cadImage.SelectedEntities[0] as CADInsert;
    cadImage.ClearSelection();
    if (ins == null) return;
    //create new layer
    CADLayer newlayer = new CADLayer
    {
        Color = Color.Blue,
        Name = "Blue_layer",
    };
    this.cadImage.Converter.Loads(newlayer);
    this.cadImage.Converter.GetSection(ConvSection.Layers).AddEntity(newlayer);
    //create insert copy 
    CADInsert cloneSelectedIns = new CADInsert();
    //using Assign, not Clone method!
    cloneSelectedIns.AssignEntity(ins);
    cloneSelectedIns.Loaded(cadImage.Converter);
    //in GDS inserts can be stored in inserts and Entities in the insert's Block that are stored on another layer, not the insert layer
    //you can iterate the entities in the block and set a new layer, entities' Color must be "color by layer"(CADConst.clByLayer).            
    SetEntsinInsertToNewLayer(cloneSelectedIns, newlayer);            
    //Add to layout
    cadImage.CurrentLayout.AddEntity(cloneSelectedIns);
    //But if you do like this (set new layer to the entities in the block ), ALL(!) inserts that refer to this block (in which you changed the layer) will change the color
    //would change color by layer
    cadPictBox.Invalidate();}

private void SetEntsinInsertToNewLayer(CADInsert ins, CADLayer newlayer)
{
    foreach (CADEntity ent in ins.Block.Entities)
    {                
        if (ent is CADInsert)
            SetEntsinInsertToNewLayer((CADInsert)ent, newlayer);
        ent.Layer = newlayer;
    }
}
In GDS files there are many inserts that refer to the same block (it's a format's peculiarity). So, when you change something in a block, all the inserts that refer to this block will be changed, too.
And if you copy one insert that refers to some block and assign some layer to it, all the inserts that refer to this block will be also changed, and not only one that you copy.
That's why if you want a new object (so that other inserts won't be changed), you should create a new block as well.

If you want to change entities' color only in your newly created insert, you should create new Block (with new name) by Cloning old block or add entities that you need to this new block, add them to Blocks Section of cadImage(in section block of dxf structure file) and create CADInsert with reference to this block.

Best wishes,
Catherine.
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

SuperCriss123
Posts: 3
Joined: 28 Nov 2023, 15:31

Re: CADInsert move to multiple layers

Post by SuperCriss123 » 12 Dec 2023, 11:02

thank you now it works fine

Post Reply