70002009 How to Partial save and load Layouts for a drawings

Article 70002009
Type HowTo
Product Engine
Version 9001
Date Added 6/22/2021 12:00:00 AM
Fixed 9.9002.0.5 (6/22/2021 12:00:00 AM)
Submitted by hicomtech

Summary

How to Partial save and load Layouts for a drawings

Solution

In the following example :
1.open a drawing that already contains some Layouts
2.Save Layouts to a separated drawing files
3.Save the drawing to single file with out Layouts
4.Open the single drawing
5.Load the exported layout files

//1.open a drawing that already contains some Layouts
                doc.Open(@"D:\Trampa\Trampa2\STAVROS\Support\partialSave\basebuilding.vdcl");
                //define the path that we will export the new files
                string exportpath = @"D:\Trampa\Trampa2\STAVROS\Support\partialSave\";
                bool compressVDCL = true;//set the serialization to compressed by default (like vdcl extension)

                //get all the handles of the drawing 
                System.Collections.SortedList handles = doc.GetSortedHandleTable();

                //create an array to keep the layout files reference the drawing
                VectorDraw.Professional.vdCollections.StringArray layoufiles = new VectorDraw.Professional.vdCollections.StringArray();
                //set drawing active layout to default Model
                doc.ActiveLayOut = doc.Model;
//2.Save Layouts to a separated drawing files 
                for (int i = 0; i < doc.LayOuts.Count; i++)
                {
                    //define the exported layout file name with our custom extension
                    string layoutfile =System.IO.Path.GetFileNameWithoutExtension(doc.FileName) + "_Layout_" + i.ToString() + ".vdlayout";
                    layoufiles.AddItem(layoutfile);
                    //create a new serializer foreach layout
                    VectorDraw.Serialize.MemorySerializer serializer = new VectorDraw.Serialize.MemorySerializer(compressVDCL ? vdFileProperties.Compression.BigDocumentSizeCompression.GetHashCode() : 0);
                    VectorDraw.Professional.vdPrimaries.vdLayout layout = doc.LayOuts[i];
                    //add the reference object to serializer that we do not want to be saved in the file They are already defined in the based drawing(like layers blocks textstyles dimstyles images etc)
                    //we only save the Layout with its Printer and Entities
                    foreach (System.Collections.DictionaryEntry item in handles)
                    {
                        if (object.ReferenceEquals(item.Value, layout)) continue;
                        if (object.ReferenceEquals(item.Value, layout.Printer)) continue;
                        if (layout.Entities.FindItem(item.Value)) continue;
                        serializer.AddReferenceObject(item.Value);
                    }
                    serializer.DoublePrecision = doc.FileProperties.DoublePrecision;
                    serializer.Serialize(layout, null);
                    serializer.Flush();
                    //get the Memory stream and save it to a file on disk
                    System.IO.MemoryStream ms = serializer.ToMemoryStream();
                    serializer.Close();
                    serializer.Dispose();

                    System.IO.FileStream efs = System.IO.File.Create(exportpath + layoutfile);
                    efs.Write(ms.GetBuffer(), 0, (int)ms.Length);
                    efs.Dispose();
                }


//3.Save the drawing to single file with out Layouts
                doc.LayOuts.RemoveAll();
                doc.Model.XProperties["Layouts"] = layoufiles;
                doc.SaveAs(exportpath + "singlemodel.vdcl");
                //B.Test Importing Layouts
//4.Open the single drawing
                doc.Open(exportpath + "singlemodel.vdcl");
                VectorDraw.Professional.vdCollections.StringArray layoufilenames = doc.Model.XProperties["Layouts"] as VectorDraw.Professional.vdCollections.StringArray;
                handles = doc.GetSortedHandleTable();//recalculate the handles for the single document with out layouts
//5.Load the exported layout files 
                for (int i = 0; i < layoufilenames.Count; i++)
                {
                    string layoutfilename;
                    if(!doc.FindFile(layoufilenames[i], out layoutfilename)) continue;
                    System.IO.FileStream efs = System.IO.File.OpenRead(layoutfilename);

                    doc.Openflags |= vdDocument.OpenFlagsEnum.RecoverDublicateHandles;
                    doc.FreezeActionsStack.Push(true);
                    doc.FreezeModifyEvents.Push(true);
                    doc.LockLayerMethodStack.Push(vdDocument.LockLayerMethodEnum.EnableAll);
                    doc.UndoHistory.PushEnable(false);
                    VectorDraw.Serialize.MemoryDeSerializer deSerializer;
                    if (compressVDCL) deSerializer = new VectorDraw.Serialize.GZIPDeSerializer(efs, doc.Activator);
                    else deSerializer = new VectorDraw.Serialize.MemoryDeSerializer(efs, doc.Activator);
                    
                    
                    foreach (System.Collections.DictionaryEntry item in handles) deSerializer.AddReferenceObject(item.Value);
                    
                    object[] objs = deSerializer.Read();

                    deSerializer.Close();
                    deSerializer.Dispose();

                    doc.FreezeActionsStack.Pop();
                    doc.FreezeModifyEvents.Pop();
                    doc.LockLayerMethodStack.Pop();
                    doc.UndoHistory.PopEnable();

                    if (objs != null && objs.Length == 1 && objs[0] is VectorDraw.Professional.vdPrimaries.vdLayout) doc.LayOuts.AddItem(objs[0]);
                }

Send comments on this topic.