←  Technical Questions

nanoCAD forum

»

Is there any similar command to AutoCAD...

Nayara Ferreira's Photo Nayara Ferreira 01 Feb 2017

To move elements from Model Space to Paper Space through Viewport.
Quote

Nayara Ferreira's Photo Nayara Ferreira 06 Feb 2017

View PostNayara Ferreira, on 01 February 2017 - 06:26 PM, said:

To move elements from Model Space to Paper Space through Viewport.

RESOLVED
I created the command
using HostMgd.ApplicationServices;
using HostMgd.EditorInput;
using System;
using Teigha.DatabaseServices;
using Teigha.Geometry;
namespace COMANDOS
{
	class CHSpace
	{
		static double escala = 1;
		public static void CHSPACE()
		{
			Document document = Application.DocumentManager.MdiActiveDocument;
			Database db = HostApplicationServices.WorkingDatabase;
			Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
			try
			{

				using (Transaction tr = db.TransactionManager.StartTransaction())
				{
					Viewport acVportTblRec;
					acVportTblRec = tr.GetObject(document.Editor.ActiveViewportId,
														OpenMode.ForWrite) as Viewport;
					if (acVportTblRec == null)
					{
						tr.Commit();
						ed.WriteMessage("Não existe nenhuma Viewport ativa.");
						return;
					}

					PromptSelectionOptions promptSelectionOptions = new PromptSelectionOptions();
					promptSelectionOptions.MessageForAdding = "Selecione os objetos:";
					PromptSelectionResult prEntRes1 = document.Editor.GetSelection(promptSelectionOptions);
					if (prEntRes1.Status != PromptStatus.OK)
					{
						tr.Commit();
						return;
					}
                    Matrix3d m = MS2PS(acVportTblRec);
					foreach (SelectedObject item in prEntRes1.Value)
					{
		
						Entity ent = tr.GetObject(item.ObjectId, OpenMode.ForWrite) as Entity;
						BlockTableRecord space = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
						Entity clone = ent.Clone() as Entity;
						space.AppendEntity(clone);
						tr.AddNewlyCreatedDBObject(clone, true);
						Scale(clone.Id, m, escala);
						ent.Erase();
					}
					tr.Commit();
				}
			}
			catch (Exception ex)
			{
				ed.WriteMessage(ex.ToString());
			}
		}
		internal static Matrix3d MS2PS(Viewport vp)
		{
			Vector3d viewDirection = vp.ViewDirection;
			Point2d center = vp.ViewCenter;
			Point3d viewCenter = new Point3d(center.X, center.Y, 0);
			Point3d viewTarget = vp.ViewTarget;
			double twistAngle = -vp.TwistAngle;
			Point3d centerPoint = vp.CenterPoint;
			double viewHeight = vp.ViewHeight;
			double height = vp.Height;
			double width = vp.Width;
			double scaling = viewHeight / height;
			double lensLength = vp.LensLength;

			Vector3d zAxis = viewDirection.GetNormal();
			Vector3d xAxis = Vector3d.ZAxis.CrossProduct(viewDirection);

			Vector3d yAxis;
			if (!xAxis.IsZeroLength())
			{
				xAxis = NormalizeVector(xAxis);
				yAxis = zAxis.CrossProduct(xAxis);
				double tmp = xAxis.X;
				tmp = xAxis.Y;
				tmp = xAxis.Z;
			}
			else if (zAxis.Z < 0)
			{
				xAxis = -Vector3d.XAxis;
				yAxis = Vector3d.YAxis;
				zAxis = -Vector3d.ZAxis;
			}
			else
			{
				xAxis = Vector3d.XAxis;
				yAxis = Vector3d.YAxis;
				zAxis = Vector3d.ZAxis;
			}
			Matrix3d ps2dcs = Matrix3d.Displacement(Point3d.Origin - centerPoint);
			ps2dcs = ps2dcs * Matrix3d.Scaling(scaling, centerPoint);
			Matrix3d dcs2wcs = Matrix3d.Displacement(viewCenter - Point3d.Origin);
			Matrix3d matCoords = Matrix3d.AlignCoordinateSystem(
			Matrix3d.Identity.CoordinateSystem3d.Origin,
			Matrix3d.Identity.CoordinateSystem3d.Xaxis,
			Matrix3d.Identity.CoordinateSystem3d.Yaxis,
			Matrix3d.Identity.CoordinateSystem3d.Zaxis,
			Matrix3d.Identity.CoordinateSystem3d.Origin,
			xAxis, yAxis, zAxis);
			dcs2wcs = matCoords * dcs2wcs;
			dcs2wcs = Matrix3d.Displacement(viewTarget - Point3d.Origin) * dcs2wcs;
			dcs2wcs = Matrix3d.Rotation(twistAngle, zAxis, viewTarget) * dcs2wcs;
			Matrix3d perspMat = Matrix3d.Identity;
			if (vp.PerspectiveOn)
			{
				double viewsize = viewHeight;
				double aspectRatio = width / height;
				double adjustFactor = 1.0 / 42.0;
				double adjustedLensLength = viewsize * lensLength * Math.Sqrt(1.0 + aspectRatio * aspectRatio) * adjustFactor;
				double eyeDistance = viewDirection.Length;
				double lensDistance = eyeDistance - adjustedLensLength;
				double ed = eyeDistance;
				double ll = adjustedLensLength;
				double l = lensDistance;
				perspMat = new Matrix3d(new double[] {
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, (ll - l) / ll, l * (ed - ll) / ll,
0, 0, -1.0 / ll, ed / ll });
			}
			return ps2dcs.Inverse() * perspMat * dcs2wcs.Inverse();
		}

		internal static Vector3d NormalizeVector(Vector3d vec)
		{
			double length = Math.Sqrt((vec.X * vec.X) + (vec.Y * vec.Y) + (vec.Z * vec.Z));
			double x = vec.X / length;
			double y = vec.Y / length;
			double z = vec.Z / length;
			return new Vector3d(x, y, z);
		}
		private static void Scale(ObjectId id, Matrix3d transform, double scale)
		{
			Database db = id.Database;
			using (Transaction tr = db.TransactionManager.StartTransaction())
			{
				try
				{
					Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
					if (ent != null)
					{
						ent.TransformBy(transform);
					}
					if (id.ObjectClass.DxfName.ToUpper() == "DIMENSION")
					{
						Dimension d = ent as Dimension;
						d.Dimscale = scale;
					}

				}
				catch (Teigha.Runtime.Exception)
				{
				}
				tr.Commit();
				tr.Dispose();
			}
		}
	}
}
Quote

Hellen_V's Photo Hellen_V 06 Feb 2017

Hello Nayara,
nanoCAD don't have the same commands, but it is in the wishlist.
Probably, we will release it.
Quote