Hello,
When I use the blockRef.TransformBy function that you recommend, the blocks are not stacked one on top of each other, but overlap. Why would this happen?
Thanks,
cannorth
Hello,
I have attached a VB.Net project for AutoCAD 2012 with a drawing. I want the project to stack the drawings one on top of each other in a drawing using a move entities function. However, the problem is when I use MoveEntities, it has an error. Can someone please point out the
problem with my coding approaching?
Thanks,
cannorth
//using Linq;
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("acad.exe", EntryPoint = "acedCmd", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
extern static private int acedCmd(IntPtr resbuf);
[CommandMethod("moveToOrig, mto", CommandFlags.UsePickSet | CommandFlags.Redraw)]
static public void testMoveToOrigin()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
ResultBuffer rb = new ResultBuffer();
try
{
rb.Add(new TypedValue(5005, "_Zoom"));
rb.Add(new TypedValue(5005, "_Extents"));
acedCmd(rb.UnmanagedObject);
Matrix3d ucs = ed.CurrentUserCoordinateSystem;
CoordinateSystem3d ccos = ucs.CoordinateSystem3d;
Point3d orig = ccos.Origin.TransformBy(Matrix3d.Identity);
// select all objects
SelectionSet sset = ed.SelectAll().Value;
if (sset == null) return;
List<Point3d> pts = new List<Point3d>();
List<Entity> ents = new List<Entity>();
Matrix3d mmx = new Matrix3d();
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
// iterate through selected objects
foreach (ObjectId id in sset.GetObjectIds())
{
Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead, false);
Extents3d ext = ent.GeometricExtents;
if (ext != null)
{
Point3d minpt = ext.MinPoint.TransformBy(Matrix3d.Identity);
// collect entities to List for the future work
ents.Add(ent);
pts.Add(minpt);
}
}
// Get most lower left point of screen
Point3d xpt = pts.OrderBy(p => p.X).First();// calculate minimal X value
Point3d ypt = pts.OrderBy(p => p.Y).First();// calculate minimal Y value
Point3d mp = new Point3d(xpt.X, ypt.Y, orig.Z).TransformBy(Matrix3d.Identity);
mmx = Matrix3d.Displacement(orig-mp );
// iterate through gathrered entities again
foreach (Entity e in ents)
{
e.UpgradeOpen();
// apply transformation matrix
e.TransformBy(mmx);
}
tr.Commit();
}
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage("\n" + ex.Message + "\n" + ex.StackTrace);
}
finally
{
rb = new ResultBuffer();
rb.Add(new TypedValue(5005, "_Zoom"));
rb.Add(new TypedValue(5005, "_Extents"));
acedCmd(rb.UnmanagedObject);
}
}
Point3d xpt = pts.OrderBy(p => p.X).First();// calculate minimal X value
Point3d ypt = pts.OrderBy(p => p.Y).First();// calculate minimal Y value
Has issues in VB.Net because the List data type does not recognize OrderBy as a method.Imports System.Linq?
Thanks,
cannorth
Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim db As Database = doc.Database
Dim rb As ResultBuffer = New ResultBuffer()
Try
rb.Add(New TypedValue(5005, "_Zoom"))
rb.Add(New TypedValue(5005, "_Extents"))
acedCmd(rb.UnmanagedObject)
Dim ucs As Matrix3d = ed.CurrentUserCoordinateSystem
Dim ccos As CoordinateSystem3d = ucs.CoordinateSystem3d
Dim orig As Point3d = pt1
' select all objects
Dim sset As SelectionSet = ed.SelectAll().Value
If (sset Is Nothing) Then
Exit Sub
End If
Dim pts As New List(Of Point3d)()
Dim ents As List(Of Entity) = New List(Of Entity)
Dim mmx As Matrix3d = New Matrix3d()
Using tr1 As Transaction = doc.TransactionManager.StartTransaction()
'iterate through selected objects
For Each id As ObjectId In sset.GetObjectIds()
Dim ent As Entity = tr1.GetObject(id, OpenMode.ForRead, False)
Dim ext As Extents3d = ent.GeometricExtents
If ext <> Nothing Then
Dim minpt As Point3d = ext.MinPoint.TransformBy(Matrix3d.Identity)
' collect entities to List for the future work
ents.Add(ent)
pts.Add(minpt)
End If
Next
' Get most lower left point of screen
Dim xpt As Point3d = pts.OrderBy(Function(p) p.X).First()
' calculate minimal X value
Dim ypt As Point3d = pts.OrderBy(Function(p) p.Y).First()
' calculate minimal Y value
Dim mp As Point3d = New Point3d(xpt.X, ypt.Y, orig.Z).TransformBy(Matrix3d.Identity)
mmx = Matrix3d.Displacement(orig - mp)
' iterate through gathrered entities again
For Each e As Entity In ents
e.UpgradeOpen()
' apply transformation matrix
e.TransformBy(mmx)
Next
tr1.Commit()
End Using
Catch ex As Exception
End Try
When I use fixo's function to move in VB.Net, it doesn't work. It doesn't move anything.
Here's my vb.net code:Code: [Select]
--------<snip>---------
Catch ex As Exception
End Try
Thanks,
cannorth