Something to play with :
Accounts Co-ordinate System other than WCS for insertion Point.
// CodeHimBelonga kdub@theSwamp 20100922
// Updated 20110312
// Updated 20110313 workaround for Import bug
// Updated 20110314 translate UCS to WCS for move.
//
using System;
using System.IO;
using System.Windows.Forms;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
//
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using AcUtils = Autodesk.AutoCAD.Internal.Utils;
[assembly: CommandClass(typeof(Ducks.MyCommands))]
namespace Ducks
{
public class MyCommands
{
[CommandMethod("ImportDXF", CommandFlags.Session)]
public void DucksIn_5()
{
Document doc = AcadApp.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
//------------------------------------------------------------------
OpenFileDialog dialog = new OpenFileDialog {
CheckFileExists = true,
CheckPathExists = true,
DefaultExt = "dxf",
DereferenceLinks = true,
Filter = "DXF Files (*.dxf)|*.dxf|All files (*.*)|*.*",
Title = "Select dxf file"
};
if(dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
return;
//------------------------------------------------------------------
PromptPointResult ppr = ed.GetPoint("Insertion point");
if(ppr.Status != PromptStatus.OK)
return;
//------------------------------------------------------------------
PromptDoubleOptions pdo = new PromptDoubleOptions("Scale factor");
pdo.AllowZero = false;
pdo.AllowNone = false;
pdo.DefaultValue = 1;
PromptDoubleResult pdr = ed.GetDouble(pdo);
if(pdr.Status != PromptStatus.OK)
return;
//------------------------------------------------------------------
string fn = dialog.FileName;
double insScale = pdr.Value;
ObjectId lastEnt = AcUtils.EntLast();
double[] baseArray = Point3d.Origin.ToArray();
AcadDocument comDoc = (Autodesk.AutoCAD.Interop.AcadDocument)doc.AcadDocument;
comDoc.Import(fn, (object)baseArray, insScale);
//------------------------------------------------------------------
ObjectIdCollection idColl = new ObjectIdCollection();
using(Transaction tr = doc.TransactionManager.StartTransaction()) {
// if the db is empty lastent will be null
if(lastEnt == ObjectId.Null) {
lastEnt = AcUtils.EntFirst();
idColl.Add(lastEnt);
}
ObjectId nextent = AcUtils.EntNext(lastEnt);
while(nextent != ObjectId.Null) {
idColl.Add(nextent);
nextent = AcUtils.EntNext(nextent);
}
Matrix3d transform = Matrix3d.Displacement(ppr.Value.UcsToWcs().GetAsVector());
foreach(ObjectId id in idColl) {
((Entity)tr.GetObject(id, OpenMode.ForWrite, true)).TransformBy(transform);
}
tr.Commit();
}
}
}
public static partial class Stuff
{
// CodeHimBelongaKwb © Feb 2008
// transposed from MgdDbg by JIM AWE.
//------------------------------------------------------------------
// predefined values for common Points and Vectors
public static readonly Point3d kOrigin = new Point3d(0.0, 0.0, 0.0);
public static readonly Vector3d kXAxis = new Vector3d(1.0, 0.0, 0.0);
public static readonly Vector3d kYAxis = new Vector3d(0.0, 1.0, 0.0);
public static readonly Vector3d kZAxis = new Vector3d(0.0, 0.0, 1.0);
//------------------------------------------------------------------
/// <summary>
/// shortcut for getting the current DWG's database
/// </summary>
/// <returns>Database for the current drawing</returns>
public static Database GetCurDwg()
{
Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
return db;
}
//------------------------------------------------------------------
/// <summary>
/// Get a transformed copy of a point from UCS to WCS
/// </summary>
/// <param name="pt">Point to transform</param>
/// <returns>Transformed copy of point</returns>
public static Point3d UcsToWcs(this Point3d pt)
{
Matrix3d m = GetUcsMatrix(GetCurDwg());
return pt.TransformBy(m);
}
//------------------------------------------------------------------
/// <summary>
/// Is Paper Space active in the given database?
/// </summary>
/// <param name="db">Specific database to use</param>
/// <returns></returns>
public static bool IsPaperSpace(this Database db)
{
System.Diagnostics.Debug.Assert(db != null);
if (db.TileMode)
return false;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
if (db.PaperSpaceVportId == ed.CurrentViewportObjectId)
return true;
return false;
}
//------------------------------------------------------------------
/// <summary>
/// Figure out the current UCS matrix for the given database. If
/// PaperSpace is active, it will return the UCS for PaperSpace.
/// Otherwise, it will return the UCS for the current viewport in
/// ModelSpace.
/// </summary>
/// <param name="db">Specific database to use</param>
/// <returns>UCS Matrix for the specified database</returns>
public static Matrix3d GetUcsMatrix(Database db)
{
System.Diagnostics.Debug.Assert(db != null);
Point3d origin;
Vector3d xAxis, yAxis, zAxis;
if(db.IsPaperSpace()) {
origin = db.Pucsorg;
xAxis = db.Pucsxdir;
yAxis = db.Pucsydir;
} else {
origin = db.Ucsorg;
xAxis = db.Ucsxdir;
yAxis = db.Ucsydir;
}
zAxis = xAxis.CrossProduct(yAxis);
return Matrix3d.AlignCoordinateSystem(kOrigin, kXAxis, kYAxis, kZAxis, origin, xAxis, yAxis, zAxis);
}
//------------------------------------------------------------------
}
}