Copy entities from source model to taget model

1
Kai
4/30/2019 10:01 AM

Hello,

here I have a further question.
How can I copy entities from a special area of a source model to another model and reference this to new koordinates.
E.g. to the reference point X = 0, Y = 0 ?

And here the code

C# Code:
int[] iFound = new int[4];
            bool BFound = false;
            int iTestDebug = 0;

            if (sBasicMake.HeightPosFpTeilTab.Length > 1)
            {
                Point3D ptTargetPos = new Point3D();
                GraphicsConfig gfConfig = new GraphicsConfig(true);
                BoundsCalculator bndCalculator = new BoundsCalculator(gfConfig);


                try
                {
                    for (int i = 0; i < sBasicMake.HeightPosFpTeilTab.Length; i++)
                    {
                        //Model vom Basismodel clonen, Dateinamen (für *dxf) im Model definieren und der Map hinzufügen
                        strTemp = String.Format("{0}_{1}", ConfigurationManager.AppSettings.Get("FronplattenTeilNr"), i + 1);
                        MyModel addmodel = MyModel.CloneNewModel(model, strTemp);
                        strTemp = string.Format("Fp_{0}", i + 1);
                        mapModel.Add(strTemp, addmodel);

                        model1 = mapModel[strTemp];

                        
                        foreach (DxfEntity entity in model.Entities)
                        {
                            bndCalculator = new BoundsCalculator(gfConfig);
                            bndCalculator.GetBounds(model, entity);
                            bndCalculator.CreateGraphicsFactory();

                            DxfHandledObject handle = new DxfHandledObject();
                            CloneContext context = new CloneContext(model, model1, ReferenceResolutionType.IgnoreMissing);

                            BFound = false;

                            if (bndCalculator.Bounds.Corner1.Y >= rectangle[i].Y1 && bndCalculator.Bounds.Corner2.Y <= rectangle[i].Y2)
                            {
                                iFound[i]++;
                                dTemp = bndCalculator.Bounds.Center.Y - rectangle[i].Y1;

                                //Wenn die Koordinaten ermittelt wurden dann ins Target Model einfügen; Ein "TransformMe" darf hier nicht mehr ausgeführt werden!
                                //entity.TransformMe(new TransformConfig(), Transformation4D.Translation(0, dTemp, 0));

                                DxfEntity m1Entity = new DxfUnknownEntity();
                                m1Entity.CopyFrom(handle, context);

                                model1.Entities.Add(entity);
                            }
                            else
                            {
                                //Prüfen ob des Entity in ein anderes Fp-Teil liegt
                                for (int j = 0; j < sBasicMake.HeightPosFpTeilTab.Length; j++)
                                {
                                    if (j == i)
                                        continue;

                                    if (bndCalculator.Bounds.Corner1.Y >= rectangle[j].Y1 && bndCalculator.Bounds.Corner2.Y <= rectangle[j].Y2)
                                        BFound = true;
                                }

                                if (!BFound)
                                {
                                    iTestDebug = 0;
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    strMsgTemp = string.Format("Systemausgabe: {0}", e.Message);
                }
            }


Thanks for help.

Wout
5/2/2019 8:52 AM

Hi,

You shouldn't create DxfHandledObject instances, this is just an abstract base class that is not useful by itself (except for some internal uses). Instead you should probably just Clone the original object, I'm not sure why you are creating a DxfHandledObject.

Similarly for DxfUnknownEntity, these are intended for reading DWG files with unknown objects that contain proxy graphics, so CadLib can still render these objects. But you should normally never create them yourself.

- Wout

Kai
5/3/2019 6:46 AM

Hello,

now it works, but not in hole way I want.
I create a clone if entity by using the class "CloneContext".

Now it accoures the next problem.
Several entities are dimensions insert as a block.
How can I add these entities to the target (here "modelTemp") model?

As followed my source code:

C# Code:
//Model vom Basismodel clonen, Dateinamen (für *dxf) im Model definieren und der Map hinzufügen
                        strTemp = String.Format("{0}_{1}", ConfigurationManager.AppSettings.Get("FronplattenTeilNr"), i + 1);
                        MyModel addmodel = MyModel.CloneNewModel(model, strTemp);
                        strTemp = string.Format("Fp_{0}", i + 1);
                        mapModel.Add(strTemp, addmodel);

                        modelTemp = mapModel[strTemp];


                        CloneContext context = new CloneContext(model, modelTemp, ReferenceResolutionType.CloneMissing);


                        foreach (DxfEntity entity in model.Entities)
                        {
                            bndCalculator = new BoundsCalculator(gfConfig);
                            bndCalculator.GetBounds(model, entity);
                            //bndCalculator.CreateGraphicsFactory();


                            BFound = false;

                            if (bndCalculator.Bounds.Initialized)
                            {
                                if (bndCalculator.Bounds.Corner1.Y >= li2DRect[i].Y1 && bndCalculator.Bounds.Corner2.Y <= li2DRect[i].Y2)
                                {
                                    iFound[i]++;

                                    DxfEntity clonedEntity = (DxfEntity)entity.Clone(context);

                                    //Wenn die Koordinaten ermittelt wurden dann ins Target Model einfügen; Ein "TransformMe" darf hier nicht mehr ausgeführt werden!
                                    //dTemp = bndCalculator.Bounds.Center.Y - li2DRect[i].Y1;
                                    //clonedEntity.TransformMe(new TransformConfig(), Transformation4D.Translation(0, dTemp, 0));
                                    if(entity.AcClass == "AcDbMInsertBlock")
                                        clonedEntity.TransformMe(new TransformConfig(), Transformation4D.Translation(0, 0, 0));

                                    modelTemp.Entities.Add(clonedEntity);
                                }
                                else
                                {
                                    //Prüfen ob des Entity in ein anderes Fp-Teil liegt
                                    for (int j = 0; j < iFpPartNo; j++)
                                    {
                                        if (j == i)
                                            continue;

                                        if (bndCalculator.Bounds.Corner1.Y >= li2DRect[j].Y1 && bndCalculator.Bounds.Corner2.Y <= li2DRect[j].Y2)
                                            BFound = true;
                                    }

                                    if (!BFound)
                                    {
                                        iTestDebug = 0;
                                    }
                                }
                            }
                            else
                            {
                                if(!liString.Contains(entity.AcClass))
                                    liString.Add(entity.AcClass);
                            }
                        }

Thanks for help

Wout
5/3/2019 4:23 PM

I'm not sure if I understand your question. If you want to clone a block (DxfBlock), you can clone that as well, and add the cloned block to the target model's DxfModel.Blocks.

Also your code does not call CloneContext.ResolveReferences(), you must call this. You have to follow the example code in the documentation very closely.

- Wout

Kai
5/8/2019 7:41 AM

Hello,

one more problem.
I didn´t found any example code.
Also the link "ResolveReferences()" doesn´t work.

See attached file.

Thanks for help

Wout
5/8/2019 12:17 PM

What do you mean "doesn't work"?

- Wout

Wout
5/8/2019 1:18 PM

If you don't give us all the needed the information like requested in the forum page header I'm not able to help you.

Reminder:

  • The problem description. In case display is giving incorrect results please point out which parts are incorrect in case of large drawings, so we don't have to search for it. But preferably strip down the drawing to the parts that are giving the problem.
  • The input data to reproduce the problem with: the DXF/DWG file. If possible strip down the input data to the parts that cause problems. This will save us time finding the problem, and the problem gets solved faster. If the data is confidential, you can send use the data by e-mail, all data is treated confidentially.
  • Code to reproduce the problem, preferably just a Console application (we just need a Program.cs). Please make sure the code you post compiles and do not post partial code snippets that do not compile. We will not investigate the problem if the code does not compile.
  • If you are posting screenshots, please mention what the screenshot represents. It can be for example GDI+ rendering, WPF rendering, exported PDF.
- Wout

Kai
5/9/2019 6:00 AM

Hello,

referring to Your chat content:
"Also your code does not call CloneContext.ResolveReferences(), you must call this. You have to follow the example code in the documentation very closely."

I mean the hyperlink in the documentation.
The hyperlink does not lead to the page on which the code sample should reside.
My objective was to get to the code example you recommended using the "ResolveReferences()" link.
Or can You send me the code example?


Thanks for help.

Wout
5/9/2019 4:22 PM

If you scroll a bit higher in the CloneContext class you will see a clone example.

- Wout

Kai
5/10/2019 6:39 AM

Hello,

now it works.
I understood that I have to call the methode "context.ResolveReferences()"
only after the foreach-loop.
This was a wrong thinking on my part, sorry.

Problem solved.

Thanks for help.

1