Active Contributor
Posts: 42
Registered: ‎11-06-2012
Message 1 of 5 (716 Views)
Accepted Solution

Offset polyline by pick point on side like autocad

716 Views, 4 Replies
02-03-2013 05:32 PM

Hi pro,


Please let me know how to offset a opened polyline by pick point on side of opened polyline.



Posts: 1,406
Registered: ‎04-09-2008
Message 2 of 5 (698 Views)

Re: Offset polyline by pick point on side like autocad

02-04-2013 02:16 AM in reply to: xdbk07

With minimum error checking. Only as idea.

using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;

[assembly: CommandClass(typeof(Rivilis.CurveUtils))]

namespace Rivilis
  public class CurveUtils
    [CommandMethod("OffsetCurve", CommandFlags.Modal)]
    public static void OffsetCurve()
      Document doc = Application.DocumentManager.MdiActiveDocument;
      Editor ed = doc.Editor;
      PromptDoubleResult resValueOff = ed.GetDistance("\nOffset value: ");
      if (resValueOff.Status != PromptStatus.OK) return;
      PromptPointResult  resPointOff = ed.GetPoint("\nOffset side: ");
      if (resPointOff.Status != PromptStatus.OK) return;
      PromptEntityOptions prCurv = new PromptEntityOptions("\nSelect curve: ");
      prCurv.SetRejectMessage("not a Curve");
      PromptEntityResult resCurv = ed.GetEntity(prCurv);
      if (resCurv.Status != PromptStatus.OK) return;
      using  (Transaction tr = doc.TransactionManager.StartTransaction()) {
        Curve curve = tr.GetObject(resCurv.ObjectId, OpenMode.ForRead) as Curve;
        if (curve != null) {
          BlockTableRecord btr = tr.GetObject(curve.BlockId, OpenMode.ForWrite) as BlockTableRecord;
          if (btr != null) {
            Point3d pDir = (Point3d)(Application.GetSystemVariable("VIEWDIR"));
            if (pDir != null) {
              Point3d pWCS = resPointOff.Value.TransformBy(ed.CurrentUserCoordinateSystem);
              double offset = IsRightDirection(curve, pWCS, pDir.GetAsVector()) ? resValueOff.Value : -resValueOff.Value;
              DBObjectCollection curvCols = curve.GetOffsetCurves(offset);
              foreach (DBObject obj in curvCols) {
                Curve subCurv = obj as Curve;
                if (subCurv != null) {
                  tr.AddNewlyCreatedDBObject(subCurv, true);
    // Detect side of point
    public static bool IsRightDirection(Curve pCurv, Point3d p, Vector3d vDir)
      Vector3d vNormal = Vector3d.ZAxis;
      if (pCurv.IsPlanar) {
        Plane plane = pCurv.GetPlane();
        vNormal = plane.Normal;
        p = p.Project(plane, vDir);
      Point3d pNear = pCurv.GetClosestPointTo(p, true);
      Vector3d vSide = p - pNear;
      Vector3d vDeriv = pCurv.GetFirstDerivative(pNear);
      if (vNormal.CrossProduct(vDeriv).DotProduct(vSide) < 0.0)
        return true;
        return false;



Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

*Expert Elite*
Posts: 1,569
Registered: ‎10-08-2008
Message 3 of 5 (689 Views)

Re: Offset polyline by pick point on side like autocad

02-04-2013 03:34 AM in reply to: Alexander.Rivilis

Nice solution

Hats off

Cheers :smileyhappy:

Active Contributor
Posts: 42
Registered: ‎11-06-2012
Message 4 of 5 (664 Views)

Re: Offset polyline by pick point on side like autocad

02-05-2013 12:05 AM in reply to: Alexander.Rivilis


Great.It worked OK.

Thank Rivilis,

Valued Contributor
Posts: 83
Registered: ‎10-07-2005
Message 5 of 5 (142 Views)

Re: Offset polyline by pick point on side like autocad

05-27-2014 10:32 AM in reply to: xdbk07

Hello Rivilis,

Thank you for the code, it works perfect with polyline and circle I tried but works inverse in case of line.

How can I solve this?



Windows 7 (x64)
AutoCAD 2012 (x64)
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.