.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Best way to automatically import blocks

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
Anonymous
1057 Views, 6 Replies

Best way to automatically import blocks

I want to import blocks all my plugins use without the end user having to manually do it. One idea is to write a .NET class which will prompt the user to select the directory the blocks are in and import all blocks in that directory. I'm wondering though if its possible to just put the .dwg files of the blocks in the .bundle folder where all my plugins are and automatically load them at startup.

Tags (3)
6 REPLIES 6
Message 2 of 7
Balaji_Ram
in reply to: Anonymous

Hi Nick,

 

I do not think there is a way to import the blocks from those drawings without user interaction and all at once.

 

The user can import the block to the current drawing from any one of those drawings using design center (ADC command)

 

If you want to automatically bring in all the blocks, a custom command should do it.

 

Passing in all the blocks to import from a drawing in WblockCloneObjects should import them all :

http://adndevblog.typepad.com/autocad/2012/05/insert-block-from-a-different-dwg-using-net-.html

 

Regards,

Balaji

 



Balaji
Developer Technical Services
Autodesk Developer Network

Message 3 of 7
fieldguy
in reply to: Anonymous

Maybe you can use a template (dwt) for this?  Are you creating new dwg files?

 

Here is an example - there are several 

http://forums.autodesk.com/t5/net/create-drawing-from-template-dwt/m-p/3698328#M31981

Message 4 of 7
Anonymous
in reply to: Balaji_Ram

Going off of your suggestion to write a custom command, I wrote this method which I adapted from a Kean Walmsley post

 

//Setting up to import
FolderBrowserDialog folderBrowser = new FolderBrowserDialog(); //Dialog to browse for a folder
DialogResult result = folderBrowser.ShowDialog(); //Getting result
if(result == DialogResult.Cancel) //Ending method on cancel
{
return;
}
string[] filesToTryToImport = Directory.GetFiles(folderBrowser.SelectedPath); //Getting all the files to try to import as blocks
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;

//Importing all the blocks
//Database sourceDb = new Database(false, true);
for (int i = 0; i < filesToTryToImport.Count(); i++)
{
if (filesToTryToImport[i].EndsWith(".dwg"))
{
try
{
db.ReadDwgFile(filesToTryToImport[i], System.IO.FileShare.Read, true, ""); // Read the DWG into the database

ed.WriteMessage("\n" + filesToTryToImport[i] + " copied successfully!");
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage("\nError during copy: " + ex.Message);
}
}
}
db.Dispose();
}

 

However, when I run it and select a folder to pull blocks from I get a ton of eRepeatedDwgRead errors suggesting it's just trying to import the same block over and over again but I'm not sure if that's actually what's happening.

Message 5 of 7
Balaji_Ram
in reply to: Anonymous

Hi Nick,

 

You should include a "Database sourceDb = new Database(false, true);" inside the for loop

and use that database to call ReadDwgFile.

 

Regards,

Balaji

 

 



Balaji
Developer Technical Services
Autodesk Developer Network

Message 6 of 7
Anonymous
in reply to: Anonymous

If i may offer another method, what i like to do, create a file with all the blocks i want to bring, inserted on any layers i want to come with. Then insert this "Source file", which brings in all the blocks and all the layers into the current drawing, then delete the source file, leaving the blocks ready to be inserted normaly.

Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Dim ed As Editor = doc.Editor
 'test if block exists
Using tr As Transaction = db.TransactionManager.StartTransaction()
Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
'insert source file if block not found
If Not bt.Has(Blockname) Then
Dim orgsource As New Point3d(0, 0, 0)
Dim filename As String = "C:\your\file\path\to source file\here.dwg"
ed.WriteMessage(vbCrLf & BlockName & " block does not exist, now inserting source file!")
Using dbdwg As New Database(False, True)
dbdwg.ReadDwgFile(filename, IO.FileShare.Read, True, "")
Dim blkid As ObjectId
blkid = db.Insert(filename, dbdwg, True)
Dim obr As New BlockReference(orgsource, blkid)
Dim obtr As BlockTableRecord = DirectCast(tr.GetObject(bt(BlockTableRecord.ModelSpace),_ OpenMode.ForWrite), BlockTableRecord)
obtr.AppendEntity(obr)
tr.AddNewlyCreatedDBObject(obr, True)
'erases source file
obr.Erase(True)
tr.Commit()
End Using
End If
End Using

 things to change: blockname, name of any block you want to bring in, or of the source file.

filename: location where you would keep the source file.

Message 7 of 7
Anonymous
in reply to: Balaji_Ram

That fixed it! Thanks so much

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


Autodesk Design & Make Report