A C# Method to Import Block Definitions into AutoCAD

173 views Asked by At

I'm trying to write a method to programmatically import blocks into a drawing so that my .NET plugin can place instances of those blocks into the drawing. Right now my method looks like this:

        /// <summary>
        /// Method to programatically import all the blocks in a given list
        /// </summary>
        public static void ImportBlocks(string[] filesToTryToImport)
        {
            //Importing all the blocks
            for (int i = 0; i < filesToTryToImport.Count(); i++)
            {
                if (filesToTryToImport[i].EndsWith(".dwg"))
                {
                    try
                    {
                        Transaction tr = _database.TransactionManager.StartTransaction();
                        DocumentLock docLock = _activeDocument.LockDocument();
                        using (tr)
                        using (docLock)
                        {
                            BlockTable blkTbl = tr.GetObject(_database.BlockTableId, OpenMode.ForRead) as BlockTable;
                            // ToDo: Add files 
                        }
                    }
                    catch (Autodesk.AutoCAD.Runtime.Exception ex)
                    {
                        _editor.WriteMessage("\nError during copy: " + ex.Message);
                    }
                }
            }
        }

And what I want is for all the blocks whose filepaths are in the string array filesToTryToImport to appear as options when I go to insert a block

enter image description here

So, for example, if One-Inch-Block wasn't originally in the list but the file path to it was passed as a string in the array passed to this method it becomes an option.

0

There are 0 answers