示例#1
0
        public static void SaveTechProsess(CamDocument camDocument)
        {
            if (camDocument.Hash == 0 && !camDocument.TechProcessList.Any())
            {
                return;
            }
            try
            {
                const int kMaxChunkSize = 127;
                using (var resultBuffer = new ResultBuffer())
                {
                    using (MemoryStream stream = new MemoryStream())
                    {
                        BinaryFormatter formatter = new BinaryFormatter();
                        formatter.Serialize(stream, camDocument.TechProcessList);
                        stream.Position = 0;
                        for (int i = 0; i < stream.Length; i += kMaxChunkSize)
                        {
                            int    length    = (int)Math.Min(stream.Length - i, kMaxChunkSize);
                            byte[] datachunk = new byte[length];
                            stream.Read(datachunk, 0, length);
                            resultBuffer.Add(new TypedValue((int)DxfCode.Text, Convert.ToBase64String(datachunk)));
                        }
                    }
                    var newHash = resultBuffer.ToString().GetHashCode();
                    if (newHash == camDocument.Hash)
                    {
                        return;
                    }

                    using (DocumentLock acLckDoc = Acad.ActiveDocument.LockDocument())
                        using (Transaction tr = Acad.Database.TransactionManager.StartTransaction())
                            using (DBDictionary dict = tr.GetObject(Acad.Database.NamedObjectsDictionaryId, OpenMode.ForWrite) as DBDictionary)
                            {
                                if (dict.Contains(DataKey))
                                {
                                    using (var xrec = tr.GetObject(dict.GetAt(DataKey), OpenMode.ForWrite) as Xrecord)
                                        xrec.Data = resultBuffer;
                                }
                                else
                                {
                                    using (var xrec = new Xrecord {
                                        Data = resultBuffer
                                    })
                                    {
                                        dict.SetAt(DataKey, xrec);
                                        tr.AddNewlyCreatedDBObject(xrec, true);
                                        //xrec.ObjectClosed += new ObjectClosedEventHandler(OnDataModified);
                                    }
                                }
                                tr.Commit();
                            }
                }
            }
            catch (Exception e)
            {
                Acad.Alert($"Ошибка при сохранении техпроцессов", e);
            }
        }
示例#2
0
 public void SetActiveDocument(Document document)
 {
     if (document != null && !_documents.ContainsKey(document))
     {
         document.CommandWillStart        += Document_CommandWillStart;
         document.BeginDocumentClose      += Document_BeginDocumentClose;
         document.ImpliedSelectionChanged += ImpliedSelectionChanged;
         _documents[document]              = new CamDocument(_techProcessFactory);
         TechProcessLoader.LoadTechProsess(_documents[document]);
     }
     Acad.ClearHighlighted();
     _camPaletteSet.SetCamDocument(document != null ? _documents[document] : null);
 }
示例#3
0
 public void SetCamDocument(CamDocument camDocument)
 {
     _camDocument            = camDocument;
     _currentTechProcessType = null;
     if (bCreateTechProcess.DropDownItems.Count == 0)
     {
         Init();
     }
     ClearParamsViews();
     treeView.Nodes.Clear();
     _camDocument?.TechProcessList.ForEach(p => CreateTechProcessNode(p));
     treeView.SelectedNode = treeView.Nodes.Count > 0 ? treeView.Nodes[0] : null;
     toolStrip.Enabled     = _camDocument != null;
     RefreshToolButtonsState();
 }
示例#4
0
        /// <summary>
        /// Загрузить технологические процессы из файла чертежа
        /// </summary>
        public static void LoadTechProsess(CamDocument camDocument)
        {
            try
            {
                using (Transaction tr = Acad.Database.TransactionManager.StartTransaction())
                    using (DBDictionary dict = tr.GetObject(Acad.Database.NamedObjectsDictionaryId, OpenMode.ForRead) as DBDictionary)
                        if (dict.Contains(DataKey))
                        {
                            using (Xrecord xRecord = tr.GetObject(dict.GetAt(DataKey), OpenMode.ForRead) as Xrecord)
                                using (ResultBuffer resultBuffer = xRecord.Data)
                                    using (MemoryStream stream = new MemoryStream())
                                    {
                                        camDocument.Hash = resultBuffer.ToString().GetHashCode();
                                        foreach (var typedValue in resultBuffer)
                                        {
                                            var datachunk = Convert.FromBase64String((string)typedValue.Value);
                                            stream.Write(datachunk, 0, datachunk.Length);
                                        }
                                        stream.Position = 0;
                                        var formatter = new BinaryFormatter {
                                            Binder = new MyBinder()
                                        };
                                        camDocument.TechProcessList = (List <ITechProcess>)formatter.Deserialize(stream);
                                    }
                        }

                if (camDocument.TechProcessList != null)
                {
                    camDocument.TechProcessList.ForEach(p => p.Setup());
                    Acad.Write($"Загружены техпроцессы: {string.Join(", ", camDocument.TechProcessList.Select(p => p.Caption))}");
                }
            }
            catch (Exception e)
            {
                Acad.Alert($"Ошибка при загрузке техпроцессов", e);
            }
        }
示例#5
0
 public void SetCamDocument(CamDocument camDocument = null)
 {
     _techProcessView.SetCamDocument(camDocument);
     //_programView.SetCamDocument(camDocument);
 }