Keep x-ref layer settings

stefanveurink68AXD
Advocate
Advocate

Keep x-ref layer settings

stefanveurink68AXD
Advocate
Advocate

So I've got some code to copy a drawing including references files. This means in the copy, the path to the reference-files have to be set to the copies of the references. 

 

In Autocad you can then simply can change the 'saved path' field in the reference manager.  

 

Tried to do this in C# with: 

Database dat = new Database(false, true);

                using (dat)
                {
                    dat.ReadDwgFile(tek.padNIEUW, System.IO.FileShare.ReadWrite, true, "");

                    using (Transaction tr = dat.TransactionManager.StartTransaction())
                    {
                        XrefGraph xg = dat.GetHostDwgXrefGraph(true);
                        int count = xg.NumNodes;
                        if (count > 1) //anders loopt die vast als er 0 xrefs zijn in een tek. 
                        {
                            for (int i = 1; i < count; i++)
                            {
                                XrefGraphNode xrNode = xg.GetXrefNode(i);
                                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(xrNode.BlockTableRecordId, OpenMode.ForWrite);
                                dat.XrefEditEnabled = true;
                                string naam = btr.Name;
                                if (btr.IsResolved == false)
                                {
                                    string nieuwpad = "";
                                    foreach (Xref xr in gebruikteXrefs)
                                    {
                                        if (xr.naam == naam)
                                        {
                                            nieuwpad = xr.vollepadNIEUW;
                                        }
                                    }
                                    btr.PathName = nieuwpad;
                                }
                            }
                            tr.Commit();
                        }
                    }
                    dat.SaveAs(tek.padNIEUW, DwgVersion.Current);
                }
            

however this isn't working correctly. it does set the new path to the new x-ref yes, but all the layersettings and stuff are forgotten. so for example, when in the drawing setting layercolor of an x-ref-layer to blue (while in the x-ref itself it is yellow), after changing the x-ref-path the setting is forgotten, so in the drawing i see an yellow layer instead of a blue. This doesn't happen when you change the path in autocad itself (so without .net). 

 

What is going wrong here? And more important how to solve this?

 

0 Likes
Reply
482 Views
6 Replies
Replies (6)

stefanveurink68AXD
Advocate
Advocate

is there something wrong with  my questions? why do none of them get answered lately?

0 Likes

ed57gmc
Mentor
Mentor

Probably because everyone's on vacation due to the holidays.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes

stefanveurink68AXD
Advocate
Advocate

Okay, maybe now holidays is over I hope some-one knows how to solve it. 

 

Like I said, problem is, when copying a drawing together with its xrefs to a new folder, the layersettings in the new drawing are lost. For example, in the original x-ref the color of a layer is blue & in the drawing containing the x-ref its set to yellow, then in the copied version the color of the layer is blue in the x-ref AND in the drawing containing the x-ref). This as well goes for viewport-overrides (off-course). Somehow this information is lost purely by changing the x-ref path. While, when doing this by hand, it doesn't happen. 


Because there's drawings with lots of viewports and lots of x-refs, PLUS you can't really identify a viewport, I neither see a way to set all the settings again, because they might differ between different viewports in the same layer. 

0 Likes

stefanveurink68AXD
Advocate
Advocate

nobody got an answer to this?

 

Isn't there a way to recognize the viewports in both drawings, so you can find which one are the same, and attach te layersettings again in the copied drawing? Guess the same viewport in original and new drawing got a different object-id or does this stay the same? 

0 Likes

jeffHGCE
Advocate
Advocate

what is XREFOVERRIDE & VISRETAIN set to in original and new drawing?

stefanveurink68AXD
Advocate
Advocate

Maybe a little late, but no, this is not the solution. 

 

So, now I'm trying to copy all the settings from the old drawing to the new one, however, it gives me an eWrongDatabase at ltrv.linetypeobjectid = lov.vpproperties.linetypeobjectid. 

which makes sense cause it's the objectid from the original drawing, however I suspect, if I copy the drawing, this would make the objectid the same?

 

So simple solution would be to just ignore it, however, then the code doesn't continue, so is there an other way to ignore this problem? 

 

Some code (it's not easy to share all of it): 

 

                    using (Transaction tr = dat.TransactionManager.StartTransaction())
                    {
                        var layoutDict = (DBDictionary)tr.GetObject(dat.LayoutDictionaryId, OpenMode.ForRead);
                        int nr = 0;
                        LayerTable tabel = (LayerTable)tr.GetObject(dat.LayerTableId, OpenMode.ForWrite);

                        foreach (DBDictionaryEntry entry in layoutDict)
                        {
                            if (entry.Key != "Model")
                            {
                                var layout = (Layout)tr.GetObject(entry.Value, OpenMode.ForWrite);
                                foreach (ObjectId vpId in layout.GetViewports())
                                {
                                    nr++;

                                    var viewport = (Autodesk.AutoCAD.DatabaseServices.Viewport)tr.GetObject(vpId, OpenMode.ForWrite);

                                    foreach (vp viewp in tek.viewports)
                                    {
                                        if (nr == viewp.nr)
                                        {
                                            viewport.ThawAllLayersInViewport();



                                            ObjectIdCollection tefreezenlayers = new ObjectIdCollection();

                                            foreach (ObjectId layerid in tabel)
                                            {
                                                LayerTableRecord ltr = tr.GetObject(layerid, OpenMode.ForWrite) as LayerTableRecord;
                                                string naam = ltr.Name;
                                                LayerViewportProperties  ltrv = ltr.GetViewportOverrides(vpId);
                                                

                                                foreach (laagmetoverrides lov in viewp.overrides)
                                                {
                                                    if (lov.laagnaam == naam)
                                                    {
                                                        ltrv.Color = lov.vpproperties.Color;
                                                        ltrv.LinetypeObjectId = lov.vpproperties.LinetypeObjectId; 
                                                        ltrv.LineWeight = lov.vpproperties.LineWeight;
                                                        ltrv.PlotStyleName = lov.vpproperties.PlotStyleName;
                                                        ltrv.PlotStyleNameId = lov.vpproperties.PlotStyleNameId;
                                                        ltrv.Transparency = lov.vpproperties.Transparency;
                                                        tefreezenlayers.Add(layerid);
                                                    }        
                                                }
                                            }
                                           viewport.FreezeLayersInViewport(tefreezenlayers.GetEnumerator()); 
                                        }
                                    }
                                }
                            }
                        }
                        tr.Commit();
                    }
0 Likes