Skip to content
This repository has been archived by the owner on Oct 11, 2023. It is now read-only.

can we merge two layer to one #423

Open
langxueya opened this issue Nov 9, 2022 · 3 comments
Open

can we merge two layer to one #423

langxueya opened this issue Nov 9, 2022 · 3 comments
Labels

Comments

@langxueya
Copy link

I have two layers .I want merge them to one .

@DavidWishengrad
Copy link

DavidWishengrad commented Nov 9, 2022

Set the current layer to any layer that will not be removed.
Add a new layername you want to use if it does not exist.
Unlock and unfreeze any layer that you may want to removed.
Check each entity in modelspace and paperspace for it's layername, if it is the layername that you wish to remove, then reassign the new layername you wish it to have.
Purge the drawing.
Save the dxf.

@langxueya
Copy link
Author

thanks, I write a function。

move a dxfObject to layerOfTarget.

I have successfully tested。

Do you have more suggestions.

    public void MoveToAnotherLayer(DxfDocument dxf, DxfObject o,Layer layerOfTarget)
    {
        
        layerOfTarget.IsLocked = false;
        layerOfTarget.IsFrozen = false;


       
        if (o is EntityObject eo)
        {
            eo.Layer.IsLocked = false;
            eo.Layer.IsFrozen = false;
            eo.Layer = layerOfTarget;
        }

       
        if (o is netDxf.Blocks.Block block)
        {
            
            block.Layer.IsLocked = false;
            block.Layer.IsFrozen = false;
            block.Layer = layerOfTarget;
            
            
            List<DxfObject> os2 = dxf.Blocks.GetReferences(block.Name);
            foreach (DxfObject o2 in os2)
            {
                MoveToAnotherLayer(dxf,o2,layerOfTarget);
            }
       }

    }

@haplokuon
Copy link
Owner

Here you have an example of how this is done. The case of changing layers is the easiest one since only entities can belong to a layer, other table objects like a linetype can belong not only to entities but also to layers, dimension styles, multiline styles, ...

DxfDocument doc = new DxfDocument();

Layer layer1 = new Layer("Layer1")
{
    Color = AciColor.Red
};

Layer layer2 = new Layer("Layer2")
{
    Color = AciColor.Blue
};

Line line1 = new Line(new Vector2(-1, -1), new Vector2(1, 1))
{
    Layer = layer1
};
Line line2 = new Line(new Vector2(-1, 1), new Vector2(1, -1))
{
    Layer = layer2
};

doc.Entities.Add(line1);
doc.Entities.Add(line2);
doc.Save("OriginalDrawing.dxf");


// use the document we created or load one of your choice
DxfDocument dxf = DxfDocument.Load("OriginalDrawing.dxf");

// combine entities in "Layer1" with entities in "Layer2"
// get a copy of the list of all objects referenced by "Layer2"
List<DxfObject> refsLayer2 = dxf.Layers.GetReferences("Layer2");
// destination layer
Layer destLayer = dxf.Layers["Layer1"];
// assign entities in "Layer2" to "Layer1"
foreach (DxfObject o in refsLayer2)
{
    if (o is EntityObject entity)
    {
        entity.Layer = destLayer;
    }
}

// optionally we can delete "Layer2",
// this method will return false if the layer cannot be removed
// this is only possible when the number of references is zero
bool removed = dxf.Layers.Remove("Layer2");

// save the modified drawing
dxf.Save("ModifiedDrawing.dxf");

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants