Message 1 of 6
Not applicable
06-24-2021
03:01 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello,
I'm trying to create a function wich create a "local" UCS based on an angle of a rectangle polyline. Associate to this custum UCS I create a View which is centered and oriented on the polyline. Here is a diagram to explain :
My issue here is to centering the view when the polyline is rotated. Here is how I compute the polyline center :
private double symbol(double number)
{
return Math.Abs(number) / number;
}
private Point2d computeCenter(Point2d A, Point2d B)
{
double xCenter = A.X + symbol(A.X) * (B.X - Math.Abs(A.X)) / 2;
double yCenter = A.Y + symbol(A.Y) * (B.Y - Math.Abs(A.Y)) / 2;
return new Point2d(xCenter, yCenter);
}
public Point2d computeCenterRectangle(Polyline pline, Point3d origin, out Vector2d delta_X, out Vector2d delta_Y)
{
var pt1 = pline.GetPoint2dAt(0);
var pt2 = pline.GetPoint2dAt(1);
var pt3 = pline.GetPoint2dAt(2);
var pt4 = pline.GetPoint2dAt(3);
delta_X = new Vector2d();
delta_Y = new Vector2d();
Point2d center;
// The point numbers are assigned in a clockwise direction
if ((pt1.X < pt3.X && pt1.Y > pt3.Y) || (pt1.X > pt3.X && pt1.Y < pt3.Y))
{
if (origin == pline.GetPoint3dAt(0))
{
delta_X = pt4 - pt1;
delta_Y = pt2 - pt1;
center = computeCenter(pt1, pt3);
}
else if (origin == pline.GetPoint3dAt(1))
{
delta_X = pt1 - pt2;
delta_Y = pt3 - pt2;
center = computeCenter(pt2, pt4);
}
else if (origin == pline.GetPoint3dAt(2))
{
delta_X = pt2 - pt3;
delta_Y = pt4 - pt3;
center = computeCenter(pt3, pt1);
}
else
{
delta_X = pt3 - pt4;
delta_Y = pt1 - pt4;
center = computeCenter(pt4, pt2);
}
}
// Point numbers are assigned counter-clockwise
else
{
if (origin == pline.GetPoint3dAt(0))
{
delta_X = pt2 - pt1;
delta_Y = pt4 - pt1;
center = computeCenter(pt1, pt3);
}
else if (origin == pline.GetPoint3dAt(1))
{
delta_X = pt3 - pt2;
delta_Y = pt1 - pt2;
center = computeCenter(pt2, pt4);
}
else if (origin == pline.GetPoint3dAt(2))
{
delta_X = pt4 - pt3;
delta_Y = pt2 - pt3;
center = computeCenter(pt3, pt1);
}
else
{
delta_X = pt1 - pt4;
delta_Y = pt3 - pt4;
center = computeCenter(pt4, pt2);
}
}
return center;
}
And here is my function wich create the UCS and the view :
public void DocumentCmd()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
var ed = AcAp.DocumentManager.MdiActiveDocument.Editor;
// Polyline parameters
ObjectId ids;
Polyline pline;
Point2d center;
// UCS paramters
Point3d origin;
string nameUcs;
// View parameters
double viewAngle;
string viewName;
// Change the current USC to the usc's selected obect
ed.Command("_.UCS", "_ob");
// Save the new USC with the layout name
nameUcs = "UCS_" + LA_Layout_Helper.nameLayout;
ed.Command("_.UCS", "_name", "_sa", nameUcs, "");
// Get the last objet selected
PromptSelectionResult rectangleSelected = ed.SelectLast();
if (rectangleSelected == null) return;
ids = rectangleSelected.Value.GetObjectIds().ElementAt(0);
using (var tr = db.TransactionManager.StartTransaction())
{
// Initialise the new SCU
UcsTableRecord ucsRecord;
// Open the UCS table for read
var ucsTable = (UcsTable)tr.GetObject(db.UcsTableId, OpenMode.ForRead);
// Check to see if the table record exists
if (ucsTable.Has(nameUcs))
{
ucsRecord = tr.GetObject(ucsTable[nameUcs], OpenMode.ForWrite) as UcsTableRecord;
}
else
{
ucsRecord = new UcsTableRecord();
try
{
ucsRecord.Name = nameUcs;
}
catch (System.Exception)
{
ucsRecord.Name = "USC_";
}
// Open the UCSTable for write
ucsTable.UpgradeOpen();
// Add the new UCS table record
ucsTable.Add(ucsRecord);
tr.AddNewlyCreatedDBObject(ucsRecord, true);
}
// Open the active viewport
ViewportTableRecord acVportTblRec;
acVportTblRec = tr.GetObject(doc.Editor.ActiveViewportId, OpenMode.ForWrite) as ViewportTableRecord;
// Display the UCS Icon at the origin of the current viewport
acVportTblRec.IconAtOrigin = true;
acVportTblRec.IconEnabled = true;
// Set the UCS current
acVportTblRec.SetUcs(ucsRecord.ObjectId);
doc.Editor.UpdateTiledViewportsFromDatabase();
// Display the name of the current UCS
UcsTableRecord uscTblRecActive;
uscTblRecActive = tr.GetObject(acVportTblRec.UcsName, OpenMode.ForRead) as UcsTableRecord;
Matrix3d newMatrix = new Matrix3d();
newMatrix = Matrix3d.AlignCoordinateSystem(Point3d.Origin,
Vector3d.XAxis,
Vector3d.YAxis, Vector3d.ZAxis,
acVportTblRec.Ucs.Origin,
acVportTblRec.Ucs.Xaxis,
acVportTblRec.Ucs.Yaxis,
acVportTblRec.Ucs.Zaxis);
// Save transformation matrix
LA_Layout_Helper.matrixLayout = new S_Matrix(newMatrix.ToString());
la_project.Layouts[la_project.Layouts.Count - 1].Matrix = Matrix_Helper.S_Matrix_BuilderFromMatrixAcad(newMatrix);
origin = acVportTblRec.Ucs.Origin;
viewAngle = (acVportTblRec.Ucs.Xaxis).GetAngleTo(Vector3d.XAxis, Vector3d.ZAxis);
viewName = "Vue_" + LA_Layout_Helper.nameLayout;
pline = (Polyline)tr.GetObject(ids, OpenMode.ForRead);
center = computeCenterRectangle(pline, origin, out Vector2d delta_X, out Vector2d delta_Y);
//réinitialise le ucs en wcs
//ed.Command("_.UCS", "");
ViewTable view;
view = tr.GetObject(db.ViewTableId, OpenMode.ForRead) as ViewTable;
// Check to see if the named view 'View1' exists
if (view.Has(viewName) == false)
{
// Open the View table for write
view.UpgradeOpen();
// Create a new View table record
ViewTableRecord viewRec = new ViewTableRecord();
viewRec.Name = viewName;
viewRec.CenterPoint = center;
viewRec.Height = Math.Abs(delta_Y.Length);
viewRec.Width = Math.Abs(delta_X.Length) + 50;
viewRec.ViewTwist = viewAngle;
// Add the new View table record to the View table and the transaction
view.Add(viewRec);
tr.AddNewlyCreatedDBObject(viewRec, true);
// Set view current
doc.Editor.SetCurrentView(viewRec);
}
tr.Commit();
}
Vector3d Xucs = db.Ucsxdir;
Vector3d Xwcs = db.Ucsxdir;
//Gives the angle of rotation of the board /WCS
LA_Layout_Helper.angleLayout = Xucs.GetAngleTo(Xwcs);
}
I think the problem comes from working in the wrong UCS but I do not see how to do otherwise. Initially I used the command :
doc.SendStringToExecute("_.PLAN ", false, false, false);
but it zooms out on the whole drawing when I just want to zoom in on a polyline.
Thank you for your help,
Alexis
Solved! Go to Solution.