Example #1
0
        private void SaveAsDxf(string source, string dest, string drawingName)
        {
            try
            {
                if (drawingName == null)
                {
                    throw new ArgumentNullException(nameof(drawingName));
                }

                _logger.AddLog(String.Format("Drawing to Open: {0}", source));

                Autodesk.AutoCAD.DatabaseServices.Database oldDb = HostApplicationServices.WorkingDatabase;

                using (Autodesk.AutoCAD.DatabaseServices.Database db =
                           new Autodesk.AutoCAD.DatabaseServices.Database(false, true))
                {
                    db.ReadDwgFile(source, FileOpenMode.OpenForReadAndWriteNoShare, true, null);

                    db.CloseInput(true);

                    HostApplicationServices.WorkingDatabase = db;

                    var dxfout = Path.Combine(dest, drawingName.Replace(".DWG", ".DXF"));

                    HostApplicationServices.WorkingDatabase = oldDb;

                    db.DxfOut(dxfout, 16, DwgVersion.Current);
                }
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
            }
        }
Example #2
0
        public static void LoadandProcessPolys()
        {
            using (DatabaseCommands commands = new DatabaseCommands())
            {
                var TemplatePath = commands.GetTemplatePath();

                #region Get Dwgs to Process

                var dwgs = commands.LoadandProcessPolys();

                if (dwgs == null)
                {
                    throw new ArgumentNullException(nameof(dwgs));
                }
                IList <DrawingStack> DwDrawingStacks = new List <DrawingStack>();
                foreach (var dwg in dwgs)
                {
                    if (DwDrawingStacks != null)
                    {
                        DwDrawingStacks.Add(dwg);
                    }
                }

                #endregion

                GDwgPath   = commands.GetGlobalDWGPath();
                GCloudPath = commands.GetGlobalPointCloudPath();



                foreach (DrawingStack dwg in DwDrawingStacks)
                {
                    m_PolylineChildren.Clear();
                    var      acDocMgr = ACAD.Application.DocumentManager;
                    Document acNewDoc = null;
                    Document acDoc    = ACAD.Application.DocumentManager.MdiActiveDocument;

                    if (acDocMgr.Count == 1)
                    {
                        acNewDoc = acDocMgr.Add(TemplatePath);
                        using (acDocMgr.MdiActiveDocument.LockDocument())
                        {
                            acDocMgr.MdiActiveDocument = acDoc;
                            acDocMgr.CurrentDocument.CloseAndDiscard();
                            acDocMgr.MdiActiveDocument = acNewDoc;
                        }
                    }

                    using (acDocMgr.MdiActiveDocument.LockDocument())
                    {
                        if (acDoc != null)

                        {
                            ACADDB.Database acDbNewDoc = acNewDoc.Database;

                            //var ed = doc.Editor;

                            string gpath = Convert.ToString(GDwgPath);

                            if (gpath != null)
                            {
                                string path = Path.Combine(gpath, dwg.PolylineDwgName);

                                if (path == null)
                                {
                                    throw new ArgumentNullException(nameof(path));
                                }


                                if (!File.Exists(path))
                                {
                                    DatabaseLogs.FormatLogs("File Not Found", path);
                                    return;
                                }

                                try
                                {
                                    using (ACADDB.Database db = new ACADDB.Database(false, true))
                                    {
                                        // Read the DWG file into our Database object

                                        db.ReadDwgFile(
                                            path,
                                            ACADDB.FileOpenMode.OpenForReadAndReadShare,
                                            false,
                                            ""
                                            );

                                        // No graphical changes, so we can keep the preview
                                        // bitmap

                                        db.RetainOriginalThumbnailBitmap = true;

                                        // We'll store the current working database, to reset
                                        // after the purge operation

                                        var wdb = ACADDB.HostApplicationServices.WorkingDatabase;
                                        ACADDB.HostApplicationServices.WorkingDatabase = db;

                                        // Purge unused DGN linestyles from the drawing
                                        // (returns false if nothing is erased)
                                        collection = Process.GetIdsByTypeTypeValue(
                                            "POLYLINE", "LWPOLYLINE", "POLYLINE2D", "POLYLINE3d");
                                        NumberofPolylines = collection.Count;
                                        Process.CopyPolylinesBetweenDatabases(db, collection);

                                        // Still need to reset the working database
                                        datetime = (DateTime)dwg.DateStamp;
                                        ACADDB.HostApplicationServices.WorkingDatabase = wdb;

                                        string output = SetOutPutFolder(dwg, commands);

                                        // PLineToLayers.ProcessLayers(collection, wdb);
                                        string fileName = System.IO.Path.GetFileNameWithoutExtension(dwg.PolylineDwgName);
                                        outdxf = Path.Combine(output, String.Format("{0}{1}", fileName, ".dxf"));
                                        outdwg = Path.Combine(output, dwg.PolylineDwgName);
                                        collection.Clear();
                                        //Before Simplification Create Breakline Data
                                        OriginalCollection =
                                            RefreshSelectionOfPolylines(collection);

                                        m_PolylineChildren = CreatePolylineChildren(OriginalCollection);
                                    }
                                }
                                catch (Exception e)
                                {
                                    DatabaseLogs.FormatLogs("Exception: {0}", e.Message);
                                }
                            }
                        }
                    }

                    PGA.SimplifyPolylines.Commands.SimplifyPolylines();

                    using (acDocMgr.MdiActiveDocument.LockDocument())
                    {
                        try
                        {
                            Document        acDocument = ACAD.Core.Application.DocumentManager.MdiActiveDocument;
                            ACADDB.Database acDbNewDoc = acDocument.Database;
                            // if (collection != null) collection.Clear();
                            //collection = RefreshSelectionOfPolylines(collection);
                            collection        = OriginalCollection;
                            NumberofPolylines = collection.Count;

                            //Check Polyline Count

                            //Change Layer Names
                            AssignNames.ChangeLayers();

                            // collection.Clear();
                            // get the copied collection of this drawing
                            ///collection = RefreshSelectionOfPolylines(collection);

                            //NumberofPolylines = collection.Count;
                            //Get the Distinct LIDAR Points
                            //var p3d = ProcessLidarFile(
                            //    PlineToPoints(collection), datetime, dwg.Hole.ToString());
                            var lidarFile = RetrieveLidarFile(datetime, dwg.Hole.ToString());

                            //Get the Nearest Neighbor to add to Point Set
                            var neighbors = IncludeIntersectingNeighbor(lidarFile, OriginalCollection);
                            Point3dCollection totalpoints = MergePoint3DCollections(lidarFile, neighbors);
                            var p5d = ProcessLidarFile(
                                PlineToPoints(collection), datetime, dwg.Hole.ToString(), totalpoints);
                            //var p5d = ProcessLidarFileSubtractRegions(
                            //  PlineToPoints(collection), datetime, dwg.Hole.ToString(), totalpoints,collection);


                            //Insert into DB

                            InsertPointCloudToDB(dwg, datetime, p5d, PlineToPoints(collection));

                            //Create all the Distinct surfaces
                            CreateTinForDistinctSurfaces(p5d, collection);

                            acDbNewDoc.SaveAs(outdwg, ACADDB.DwgVersion.Current);
                            acDbNewDoc.DxfOut(outdxf, 16, ACADDB.DwgVersion.Current);
                        }
                        catch (ACADRT.Exception ex)
                        {
                            DatabaseLogs.FormatLogs("Exception: {0}", ex.Message);
                        }
                    }
                }
            }
        }
Example #3
0
        private void DeletePoly2D3DObjects(string source, string dest, string drawingName)
        {
            bool eraseOrig = true;

            try
            {
                if (drawingName == null)
                {
                    throw new ArgumentNullException(nameof(drawingName));
                }

                _logger.AddLog(String.Format("Drawing to Open: {0}", source));


                Autodesk.AutoCAD.DatabaseServices.Database oldDb = HostApplicationServices.WorkingDatabase;

                using (Autodesk.AutoCAD.DatabaseServices.Database db =
                           new Autodesk.AutoCAD.DatabaseServices.Database(false, true))
                {
                    db.ReadDwgFile(source, FileOpenMode.OpenForReadAndWriteNoShare, true, null);

                    db.CloseInput(true);

                    HostApplicationServices.WorkingDatabase = db;

                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        // Collect our exploded objects in a single collection
                        var selected = BBC.Common.AutoCAD.AcadUtilities.
                                       GetAllObjectIdsInModel(db, false);

                        DBObjectCollection objs = new DBObjectCollection();

                        // Loop through the selected objects

                        foreach (ObjectId so in selected)
                        {
                            // Open one at a time

                            Entity ent =
                                (Entity)tr.GetObject(
                                    so,
                                    OpenMode.ForRead
                                    );
                            ;

                            if (ent.GetType() == typeof(Polyline3d) ||
                                ent.GetType() == typeof(Polyline2d) ||
                                ent.GetType() == typeof(Acad.Polyline) ||
                                ent.GetType() == typeof(Line))
                            {
                                if (eraseOrig)
                                {
                                    ent.UpgradeOpen();
                                    ent.Erase();
                                }
                            }
                        }
                        tr.Commit();
                    }

                    var output = Path.Combine(_sdir, "S-" + drawingName + ".DWG");
                    var dxfout = output.Replace(".DWG", ".DXF");
                    db.SaveAs(output, DwgVersion.Current);

                    HostApplicationServices.WorkingDatabase = oldDb;

                    db.DxfOut(dxfout, 16, DwgVersion.Current);
                }
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
            }
        }