예제 #1
0
        ///<summary>
        /// Copies an External Reference from an existing location to another folder
        /// will overwrite any file with the same name
        ///</summary>
        public static void Xref_ExporterCopy(BlockTableRecord x)
        {
            Document doc = global::Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db  = doc.Database;

            //***Gets the current directory by finding the file path
            string curloc = Path.GetDirectoryName(db.Filename);

            //Creates a new directory for all files to be saved to
            //called "_Setup Files" and "Xrefs"
            string NewFol = "_Setup Files";
            string coploc = System.IO.Path.Combine(curloc, NewFol);

            //Creates the directory if it doesn't already exist
            Setup_Methods.Directory_Creator(coploc);

            if (!File.Exists(Path.Combine(curloc, NewFol, x.Name)))
            {
                // make a list of all the drawing files in every directory
                // in the path leading up the directory the main file lives
                try
                {
                    string[] allpossiblexrefs = Directory.EnumerateFiles(
                        (Path.GetDirectoryName(curloc)),
                        "*.dwg",
                        SearchOption.AllDirectories).ToArray <string>();
                    // if the file is in the array
                    // copy the file from its location out to copy location

                    var copy_xref = from file in allpossiblexrefs
                                    where Path.GetFileName(file).Equals((x.Name + ".dwg"))
                                    select file;
                    if (copy_xref != null)
                    {
                        foreach (var file in copy_xref)
                        {
                            File.Copy(file, (Path.Combine(coploc, (x.Name + ".dwg"))), true);
                        }
                    }
                    else
                    {
                        Xref_Exporter(x);
                    }
                }

                catch (DirectoryNotFoundException)
                {
                    //remember to add some code here and figure out
                    //how to look through all the directories better

                    // If there were any xrefs that weren't within the main directory
                    // this method will Wblock them out to a base location
                    Xref_Exporter(x);
                }
            }
            else
            {
                Editor ed = doc.Editor;
                ed.WriteMessage("\n {0} has already been exported to the Xrefs folder.", x.Name);
            }
        }
예제 #2
0
        /// <summary>
        /// Exports a BlockTableRecord out to another location using a WBlock methodology
        /// </summary>
        /// <param name="btr"> The External Reference to be exported; must be a Block Table Record </param>
        public static void Xref_Exporter(BlockTableRecord btr)
        {
            //Access the current document
            Document Cdoc   = global::Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database Adbase = Cdoc.Database;

            //***Gets the current directory by finding the file path
            string FilePath = Path.GetDirectoryName(Adbase.Filename);

            //Creates a new directory for all files to be saved to
            //called "_Setup Files" and "Xrefs"
            string NewFol    = "_Setup Files";
            string NewSetup  = "SETUP";
            string NewXrPath = null;
            string XrPath    = btr.PathName;
            string XrOrigin  = btr.Origin.ToString();
            string XrName    = btr.Name.ToString().ToUpper();

            Setup_Methods.Directory_Creator(FilePath, NewFol, NewSetup);

            //if the Xref doesn't already exist in a xref/setup folder export it there
            if (!File.Exists(Path.Combine(FilePath, NewFol, NewSetup, XrName)))
            {
                NewXrPath = Path.Combine(FilePath, NewFol, NewSetup);
                NewXrPath = Path.Combine(NewXrPath, XrName);

                try
                {
                    // if the xref in question can't be found in CAD
                    // i.e. because the xref is set to Absolute instead of Relative
                    // or because the xref is in another place for some reason
                    // search in all directories and try to resolve the xref
                    if (!btr.IsResolved)
                    {
                        Adbase.ResolveXrefs(true, false);
                    }

                    //if the xref has been found
                    if (btr.IsResolved)
                    {
                        //writes the xref out to a base location with its same name
                        using (Transaction tr = Adbase.TransactionManager.StartTransaction())
                        {
                            using (Database newDb = btr.GetXrefDatabase(true))
                            {
                                ObjectIdCollection idCol = new ObjectIdCollection();
                                IdMapping          idMap = new IdMapping();
                                Adbase.WblockCloneObjects(idCol, btr.ObjectId, idMap, DuplicateRecordCloning.Ignore, false);
                                tr.Commit();
                                if (!Directory.Exists(NewXrPath))
                                {
                                    Directory.CreateDirectory(Path.GetDirectoryName(NewXrPath));
                                }

                                //adds an .dwg to the end of the path if there isn't any
                                if (!Path.HasExtension(NewXrPath))
                                {
                                    NewXrPath += ".dwg";
                                }
                                newDb.SaveAs(NewXrPath, DwgVersion.AC1021);
                            }
                        }
                    }
                    else
                    {
                        // Write code to export the name of the file that is necessary out to the Xrefs area
                        // in a txt file preferably
                        Editor ed = Cdoc.Editor;
                        ed.WriteMessage("\n {0} was not found, ask the client to send it.", btr.Name);
                    }
                }

                catch (global::Autodesk.AutoCAD.Runtime.Exception aex)
                {
                    if (aex.ErrorStatus == ErrorStatus.InvalidKey)
                    {
                        // Write code to export the name of the file that is necessary out to the Xrefs area
                        // in a txt file preferably
                        Editor ed = Cdoc.Editor;
                        ed.WriteMessage("\n {0} was not found, ask the client to send it.", btr.Name);
                    }
                }
            }
        }