Jump to content

Recommended Posts

Posted

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

Posted

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,

Posted

Thanx msasu, I would appreciate the VBA code. I think I should be able to figure it out.

Posted

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,

Posted

Also, can get line’s orientation directly from the entity:

 

Alfa = [color=blue]theLineEntity[/color].Angle

 

Regards,

Posted

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();
           }
       }
   }
}

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...