public ObjectId InsertDwg(string fileName, ref string blockName)
{
Editor se = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
try
{
if (fileName == "" || !File.Exists(fileName))
{
se.WriteMessage("File not found");
return ObjectId.Null;
}
if (!fileName.ToLower().EndsWith(".dwg"))
{
se.WriteMessage("Not dwg file");
return ObjectId.Null;
}
}
catch (System.Exception ex)
{
se.WriteMessage(ex.StackTrace);
return ObjectId.Null;
}
Database newDB = new Database(false, false);
bool newDBHasDispose = false;
try
{
Database myDb = HostApplicationServices.WorkingDatabase;
if (blockName == "")
{
FileInfo fileInfo = new FileInfo(fileName);
blockName = fileInfo.Name.Substring(0, fileInfo.Name.Length - 4);
}
//newDB.ReadDwgFile(fileName, System.IO.FileShare.Read, true, fileName);
newDB.ReadDwgFile(fileName, System.IO.FileShare.Read, true, null);
if (newDB.Insbase.GetAsVector().Length > 0.00001)
{
if (newDB.Insbase.X >= newDB.Extmin.X && newDB.Insbase.X <= newDB.Extmax.X
&& newDB.Insbase.Y >= newDB.Extmin.Y && newDB.Insbase.Y <= newDB.Extmax.Y)
{
//Insbase位于图形范围,不进行移动,有可能不符合预期
return myDb.Insert(blockName, newDB, true);
}
else
{
if (newDB.Extmin.X > newDB.Extmax.X)
{
//图形范围Extmin Extmax未初始化,不进行移动,有可能不符合预期
return myDb.Insert(blockName, newDB, true);
}
else
{
Matrix3d tMat = Matrix3d.Displacement(newDB.Insbase.GetAsVector().Negate());
newDB.Insbase = Point3d.Origin;
ObjectId blockId = myDb.Insert(blockName, newDB, true);
if (0 >= newDB.Extmin.X && 0 <= newDB.Extmax.X && 0 >= newDB.Extmin.Y && 0 <= newDB.Extmax.Y)
{
//坐标原点位于图形范围,不进行移动,有可能不符合预期
return blockId;
}
else
{
//Insbase不位于图形范围 坐标原点也不位于图形范围,将图形从Insbase移到原点,有可能不符合预期
using(Transaction ctrans = myDb.TransactionManager.StartTransaction())
{
BlockTableRecord cbtr = (BlockTableRecord)ctrans.GetObject(blockId, OpenMode.ForWrite);
foreach (ObjectId tId in cbtr)
{
Entity ent = (Entity)ctrans.GetObject(tId, OpenMode.ForWrite);
ent.TransformBy(tMat);
}
ctrans.Commit();
}
return blockId;
}
}
}
}
else
{
//Insbase位于原点,不用做任何处理
[color=red]return myDb.Insert(blockName, newDB, true); //An exception is thrown out here, exception message:eSelfReference[/color] }
}
catch (System.Exception ex)
{
se.WriteMessage(ex.StackTrace + "\n" + ex.Message + "\n" + ex.TargetSite);
return ObjectId.Null;
}
finally
{
if (!newDBHasDispose)
newDB.Dispose();
}
}
And this is the function I call the InsertDwg(string fileName, ref string blockName).[CommandMethod("IB")]
public void InsertBlock()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
try
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
//ImportBlock_DB("test.dwg", "sample");
string blockName = "sample"; InsertDwg("test.dwg", ref blockName);
trans.Commit();
}
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.StackTrace + "\n" + ex.Message + "\n" + ex.TargetSite);
return;
}
}
string blockName = "sample"; InsertDwg("c:\\TestTwo.dwg", ref blockName);
.Acad::ErrorStatus insert(
AcDbObjectId& blockId,
const ACHAR* pBlockName,
AcDbDatabase* pDb,
bool preserveSourceDatabase = true
);
Parameters
AcDbObjectId& blockId
Input object ID for the new block table record created by this function
const ACHAR* pBlockName
Input name to be used by the new block table record created by this function
AcDbDatabase* pDb
Input pointer to database to insert entities from
bool preserveSourceDatabase = true
Input to determine whether the source database pDb will be left intact
Description
This function mimics the AutoCAD INSERT command. First a new block table record is created in the database executing this function. This new block table record is given the name pointed to by pBlockName and the blockId argument is filled in with its object ID. Then, all the Model Space entities in the database pointed to by pDb are copied into the new block table record.
The preserveSourceDatabase argument determines whether the source database pDb is left intact or not. The default value of the argument is true. In that case, objects from the source database are copied to the destination database and the source database is not changed. If the caller passes false for this argument, objects from the source database could be physically moved into the destination database. The latter runs faster, but it leaves the source database dependent on the destination database. The caller should make sure that the source database is deleted either immediately or at least before the destination database is deleted.
When preserveSourceDatabase is false, the deep clone context is AcDb::kInsert during the cloning that occurs during AcDbDatabase::insert(). When preserveSourceDatabase is true, the deep clone context is AcDb::kInsertCopy during the cloning that occurs during AcDbDatabase::insert().
Returns Acad::eOk if operation is successful.
Returns Acad::eSelfReference (and does not complete the insert process) if the database pointed to by pDb contains a block table record with the name pBlockName, even if that block table record is not referenced by entities in the pDb Model Space.
public static ObjectId m_ImportBlock(string fileName, string blockName, bool bReplace)
{
ObjectId destId = m_GetBlockId(blockName);
if (destId.IsNull)
{
using (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())//记住一定要先锁定文档
{
using (Database sourceDb = new Database(false, false))
{
sourceDb.ReadDwgFile(fileName.Trim(), FileShare.Read, true, null);
//destId = mCommands.m_db.Insert(blockName, sourceDb, false);//错误?!?!块自参照???!!!
destId = m_ImportBlock(sourceDb, blockName, bReplace);
sourceDb.CloseInput(true);
}
}
}
return destId;
}
public static ObjectId m_ImportBlock(Database sourceDb, string blockName, bool bReplace)
{
ObjectId destId = ObjectId.Null;
using (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
{
using (Transaction sourceTr = sourceDb.TransactionManager.StartTransaction())
{
BlockTable sourceBt = sourceTr.GetObject(sourceDb.BlockTableId, OpenMode.ForRead) as BlockTable;
if (sourceBt.Has(blockName))
{
ObjectId sourceId = sourceBt[blockName];
IdMapping idMap = new IdMapping();
sourceDb.WblockCloneObjects(
new ObjectIdCollection(new ObjectId[] { sourceId }),
HostApplicationServices.WorkingDatabase.BlockTableId,
idMap,
bReplace == true ? DuplicateRecordCloning.Replace : DuplicateRecordCloning.MangleName,
false);
destId = idMap[sourceId].Value;
}
sourceTr.Commit();
}
}
return destId;
}
public static ObjectId m_GetBlockId(string BlockName)
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
ObjectId blockId = ObjectId.Null;
using (Transaction m_tr = db.TransactionManager.StartTransaction())
{
BlockTable m_bt = (BlockTable)m_tr.GetObject(db.BlockTableId, OpenMode.ForRead);
if (m_bt.Has(BlockName)) blockId = m_bt[BlockName];
//else
//{
// MessageBox.Show(string.Format("指定块名\"{0}\"不存在!", BlockName));
//}
m_tr.Commit();
}
return blockId;
}
I don't use the SDK, I set the configuration manually.I think you may be misunderstanding, the ARX SDK (http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=785550) includes all the documentation on the methods you are trying to use and often help solve problems
Now, I am trying to specify the insert point to coincide with the blocks' insertpoint.I'm not quite sure what you mean here, but if you want to move the objects around inside the block definition then you'll have to do something like