Acad2012 vs 2010
I tried using Fixo's code to insert a remote .dwg into the current drawing.
Works fine so I tried to loop through multiple drawings and insert the same block.
Does the first drawing fine then it runs away after the first drawing.
I've tried promptPointResults and other methods to get the insertion point before the code can run but it just runs away, importing the block,
inserting the reference at Point3d origin, saving the dwg, then goes to the next drawing. Ignoring the delegate prompts.
Is it possible to loop through drawings from a list like this and run code? or am I trying the impossible?
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace AddDeptTag
{
public class FindTEXT
{
/// <summary>
/// AddDeptTag, opens drawings listed.
/// Inserts dept tag into drawing.
/// </summary>
///
private string filePath = "G:\\AutoCAD2012Support\\LispText\\"; //path.
private string blockFile = "G:\\AutoCAD2012Support\\LispBlocks\\WBAY.dwg";
[CommandMethod("AddDept", CommandFlags.Session)]
public void AddDeptTagToDrawings()
{
if (MessageBox.Show("This program reads PartsList.txt, opens each part drawing in list and inserts\n" +
"Department Tage into drawing.\n" +
"Please check the text file so it is currently what you want to add tag to.\n" +
"If you are uncertain to what this program does, Exit Now!\n" +
"To continue, hit Yes, to Cancel, hit No.", "Replace Text", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
string filename = filePath + "PartsList.txt"; //list of aluminum parts.
string blkName;
Point3d ptLoc = Point3d.Origin;
if (File.Exists(filename))
{
try
{
string[] lines = File.ReadAllLines(filename); //array of filename strings.
for (int i = 0; i < lines.Length; i++)
{
string str = lines[i];
Document docToInsert = AcadApp.DocumentManager.Open(str, false);
Document document = AcadApp.DocumentManager.MdiActiveDocument;
Editor editor = document.Editor;
Database db = document.Database;
//db.UpdateExt(true);
Point3d DwgExtMin = db.Extmin;
Point3d DwgExtMax = db.Extmax;
using (docToInsert.LockDocument())
{
using (Transaction transaction = docToInsert.TransactionManager.StartTransaction())
{
BlockTable table = (BlockTable)transaction.GetObject(db.BlockTableId, OpenMode.ForWrite);
BlockTableRecord btr = (BlockTableRecord)transaction.GetObject(table[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
Point3d extMin;
Point3d extMax;
Matrix3d scaleMat = new Matrix3d();
using (Database database = new Database(false, false))
{
blkName = Path.GetFileNameWithoutExtension(blockFile);
database.ReadDwgFile(blockFile, System.IO.FileShare.ReadWrite, false, "");
database.UpdateExt(true);
extMin = database.Extmin;
extMax = database.Extmax;
db.Insert(blkName, database, true);
}
if (table.Has(blkName))
{
/*if (DwgExtMin.DistanceTo(DwgExtMax) > extMin.DistanceTo(extMax))
{
scaleMat = Matrix3d.Scaling(DwgExtMin.DistanceTo(DwgExtMax) / extMin.DistanceTo(extMax) * 0.125, Point3d.Origin);
}*/
BlockReference bref = new BlockReference(Point3d.Origin, table[blkName]);
btr.AppendEntity(bref);
transaction.AddNewlyCreatedDBObject(bref, true);
//bref.TransformBy(scaleMat);
//Extents3d ext = bref.GeometricExtents;
PromptSelectionResult psr = editor.SelectLast();
SelectionSet ssLast = psr.Value;
if (psr.Status == PromptStatus.OK)
{
PromptPointResult ppr = editor.Drag( ssLast,"\nPick point to insert block " + blkName + " : ",
delegate(Point3d pt, ref Matrix3d mat)
{
if (ptLoc == pt)
return SamplerStatus.NoChange;
else
{
mat = Matrix3d.Displacement(ptLoc.GetVectorTo(pt));
}
return SamplerStatus.OK;
}
);
if (ppr.Status == PromptStatus.OK)
{
Matrix3d mat = Matrix3d.Displacement(ptLoc.GetVectorTo(ppr.Value));
bref.TransformBy(mat * scaleMat);
transaction.TransactionManager.QueueForGraphicsFlush();
}
}
}
transaction.Commit();
}
}
docToInsert.CloseAndSave(str);
} //foreach file in list of files.
} //try block
catch (System.Exception exception)
{
MessageBox.Show("\nError: " + exception);
}
} //if file exist.
else
{
MessageBox.Show("\nCheck if all files exist.\n");
}
}
} //dialog box yes/no to start program.
}
}
/* PromptPointOptions ppo = new PromptPointOptions("\nPick insertion point.");
ppo.AllowNone = false;
ppo.BasePoint = ptLoc;
ppo.UseBasePoint = true;
ppo.UseDashedLine = true;
ppo.Message = "\nPick a point to insert Block: " + blkName + " : ";
PromptPointResult ppr = editor.GetPoint(ppo);
if (ppr.Status == PromptStatus.OK)
{*/