Jozi68 Posted April 1, 2010 Posted April 1, 2010 I need to write code to change the length of a line, almost like the LEN Total command. I have the line and the startpoint. Any ideas? It has to be in vb.net Quote
MSasu Posted April 1, 2010 Posted April 1, 2010 Can try the following approach: list the start and respectively end points; calculate line's angle from X axis; recalculate location of new end point using sine and cosine of angle and the desired length; update the end point attribute of line. Done! Can post for you the VBA code but I'm afraid that is quite different than VSTA one. Regards, Quote
Jozi68 Posted April 1, 2010 Author Posted April 1, 2010 Thanx msasu, I would appreciate the VBA code. I think I should be able to figure it out. Quote
MSasu Posted April 1, 2010 Posted April 1, 2010 Here it is (variables not declared): LinePoint1st = [color=blue]theLineEntity[/color].StartPoint 'list extremities LinePoint2nd = [color=blue]theLineEntity[/color].EndPoint DeltaX = LinePoint2nd(0) - LinePoint1st(0) 'line size along axis DeltaY = LinePoint2nd(1) - LinePoint1st(1) If DeltaX = 0 Then 'calculate angle from X axe (avoid zero division) Alfa = Atn(DeltaY / DeltaX) Else If DeltaY > 0 Then Alfa = 0.5 * pi '90 degrees, positive Else Alfa = -0.5 * pi '90 degrees, negative End If End If NewDeltaX = [color=blue]NewLineLength[/color] * Cos(Alfa) 'new sizes along axis NewDeltaY = [color=blue]NewLineLength[/color] * Sin(Alfa) Set NewLinePoint2nd = LinePoint1st 'calculate new end point NewLinePoint2nd(0) = NewLinePoint1st(0) + NewDeltaX NewLinePoint2nd(1) = NewLinePoint1st(1) + NewDeltaX theLineEntity.StartPoint = NewLinePoint2nd 'update line entity Regards, Quote
MSasu Posted April 1, 2010 Posted April 1, 2010 Also, can get line’s orientation directly from the entity: Alfa = [color=blue]theLineEntity[/color].Angle Regards, Quote
gile Posted April 5, 2010 Posted April 5, 2010 Hi, IMO, writing .NET from VBA coding isn't a good way to learn .NET. .NET provides much more APIs than COM to deal with geometry issues (i.e. using 3d vectors rather than 2d angles). Here's a little sample (C#). It contains a function (LineTotalLength) which changes the total length of a line on the picked side (requieres the line ObjectId, the picked point and the length) and a 'Test' command. using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Runtime; namespace LengthenSample { public class Class1 { [CommandMethod("Test")] public void Test() { Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; PromptEntityOptions peo = new PromptEntityOptions("\nSelect a line: "); peo.SetRejectMessage("Selected entitiy is not a line"); peo.AddAllowedClass(typeof(Line), true); PromptEntityResult per = ed.GetEntity(peo); if (per.Status != PromptStatus.OK) return; PromptDistanceOptions pdo = new PromptDistanceOptions("\nTotal length: "); pdo.AllowNegative = false; pdo.AllowZero = false; PromptDoubleResult pdr = ed.GetDistance(pdo); if (pdr.Status == PromptStatus.OK) LineTotalLength(per.ObjectId, per.PickedPoint.TransformBy(ed.CurrentUserCoordinateSystem), pdr.Value); } private void LineTotalLength(ObjectId id, Point3d pt, double len) { Database db = Application.DocumentManager.MdiActiveDocument.Database; using (Transaction tr = db.TransactionManager.StartTransaction()) { Line l = (Line)tr.GetObject(id, OpenMode.ForWrite); Point3d sPt = l.StartPoint; Point3d ePt = l.EndPoint; if (pt.DistanceTo(sPt) < pt.DistanceTo(ePt)) { Vector3d vec = ePt.GetVectorTo(sPt).GetNormal(); l.StartPoint = ePt + vec * len; } else { Vector3d vec = sPt.GetVectorTo(ePt).GetNormal(); l.EndPoint = sPt + vec * len; } tr.Commit(); } } } } Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.