polyline from ucs to wcs(copy from ucs to wcs) using autocad api c#

polyline from ucs to wcs(copy from ucs to wcs) using autocad api c#

Dean.kichenbrand
Contributor Contributor
1,538 Views
4 Replies
Message 1 of 5

polyline from ucs to wcs(copy from ucs to wcs) using autocad api c#

Dean.kichenbrand
Contributor
Contributor

I Have a lot of polylines that are flat on UCS but not it WCS If I copy and paste from UCS to WCS it works perfectly but I need to do it for all polyline so I'm trying to write a script in c# to do it for me I got to a point where I can extract all the polyline to a new DWGbut I'm not understanding how to rotate them  I know the Matrix3d value should be

origin(0,0,0), Xaxis(1,0,0), Yaxis(0,1,0) and Zaxis(0,0,1) for it to be flat.

 

If there is an easier way to do this? I have found ways to align UCS to the polyline which did not help.

I have tried coping from UCS to WCS but the API's coping keeps the same UCS.

 

can I extract the polylines and recreate them on WCS top plane?

 

PS I have to use WCS because the program I'm importing from doesn't give you an option to import using UCS only WCS.

 

Any Help would be much appreciated.

Thank you

 

 

 

0 Likes
Accepted solutions (2)
1,539 Views
4 Replies
Replies (4)
Message 2 of 5

Yong.Jing
Autodesk Support
Autodesk Support

As this issue is about aip c#, I will this thread to customization forum. 



Yong Jing
Product Support Senior Specialist
Technical Support, CSS, GPS

If my post answers your question, please click the "Accept as Solution" button. This helps everyone find answers more quickly!
如果我的回帖解决了您的问题,请点击 "接受为解决方案" 按钮. 这可以帮助其他人更快的找到解决方案!
0 Likes
Message 3 of 5

marko_ribar
Advisor
Advisor
Accepted solution

Hi, this may look like random scatter and rotate, but it changes lwpolys from 3D planes to WCS...

You have to then manually align them the way you want to be in WCS, position and rotate...

 

(defun c:normalizelwpolys ( / unit v^v invm imat ss i lw n x y z elev m )

  (vl-load-com)

  (defun unit ( v / d )
    (if (not (equal (setq d (distance '(0.0 0.0 0.0) v)) 0.0 1e-8))
      (mapcar '(lambda ( x ) (/ x d)) v)
    )
  )

  (defun v^v ( u v )
    (list
      (- (* (cadr u) (caddr v)) (* (caddr u) (cadr v)))
      (- (* (caddr u) (car v)) (* (car u) (caddr v)))
      (- (* (car u) (cadr v)) (* (cadr u) (car v)))
    )
  )

  ;; Matrix Inverse  -  gile & Lee Mac
  ;; Uses Gauss-Jordan Elimination to return the inverse of a non-singular nxn matrix.
  ;; Args: m - nxn matrix

  (defun invm ( m / c f p r )
      
      (defun f ( p m )
          (mapcar (function (lambda ( x ) (mapcar (function (lambda ( a b ) (- a (* (car x) b)))) (cdr x) p))) m)
      )
      (setq  m (mapcar (function append) m (imat (length m))))
      (while m
          (setq c (mapcar (function (lambda ( x ) (abs (car x)))) m))
          (repeat (vl-position (apply 'max c) c)
              (setq m (append (cdr m) (list (car m))))
          )
          (if (equal 0.0 (caar m) 1e-14)
              (setq m nil
                    r nil
              )
              (setq p (mapcar (function (lambda ( x ) (/ (float x) (caar m)))) (cdar m))
                    m (f p (cdr m))
                    r (cons p (f p r))
              )
          )
      )
      (reverse r)
  )

  ;; Identity Matrix  -  Lee Mac
  ;; Args: n - matrix dimension

  (defun imat ( n / i j l m )
      (repeat (setq i n)
          (repeat (setq j n)
              (setq l (cons (if (= i j) 1.0 0.0) l)
                    j (1- j)
              )
          )
          (setq m (cons l m)
                l nil
                i (1- i)
          )
      )
      m
  )

  (if (setq ss (ssget "_:L" '((0 . "LWPOLYLINE"))))
    (repeat (setq i (sslength ss))
      (setq lw (ssname ss (setq i (1- i))))
      (setq n (cdr (assoc 210 (entget lw))))
      (setq x (if (equal n '(0.0 0.0 1.0) 1e-6) '(1.0 0.0 0.0)))
      (setq y (if (equal n '(0.0 0.0 1.0) 1e-6) '(0.0 1.0 0.0)))
      (if (and (null x) (equal n '(0.0 0.0 -1.0) 1e-6))
        (setq x '(-1.0 0.0 0.0))
      )
      (if (and (null y) (equal n '(0.0 0.0 -1.0) 1e-6))
        (setq y '(0.0 1.0 0.0))
      )
      (if (null x)
        (setq x (unit (v^v '(0.0 0.0 1.0) n)))
      )
      (if (null y)
        (setq y (unit (v^v n x)))
      )
      (setq z n)
      (setq elev (cdr (assoc 38 (entget lw))))
      (setq m (list (list (car x) (car y) (car z) 0.0) (list (cadr x) (cadr y) (cadr z) 0.0) (list (caddr x) (caddr y) (caddr z) 0.0) (list 0.0 0.0 0.0 1.0)))
      (vla-transformby (vlax-ename->vla-object lw) (vlax-tmatrix (invm m)))
      (vla-move (vlax-ename->vla-object lw) (vlax-3d-point (list 0.0 0.0 elev)) (vlax-3d-point '(0.0 0.0 0.0)))
    )
  )
  (princ)
)

 

HTH.

If this answer satisfies your problem, mark it as solution...

Thanks, M.R.

Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes
Message 4 of 5

Dean.kichenbrand
Contributor
Contributor
Accepted solution

Hi, Thanks for the reply

 

Thanks for the code.

I'm not sure is this C#?

 

I have Figured out a way around my problem.

What I did was get polyline data through user selection and then recreate that same polyline on a new drawing but setting the normal parameter of the new Polyline to (0,0,1) flat on WCS so now when I import the new DWG its flat.

 

Code Below:

 

PS this code is only working on one Polyline at the moment

 public class Class1
    {

        [CommandMethod("DrawP", CommandFlags.UsePickSet)]
        public void GetPline()
        {
            Editor Ced = Application.DocumentManager.MdiActiveDocument.Editor;
            Database Cdb = HostApplicationServices.WorkingDatabase;
            Transaction Ctr = Cdb.TransactionManager.StartTransaction();

            using (Ctr)
            {
                // Build a filter list so that only
                // polyline are selected

                TypedValue[] filList = new TypedValue[1] { new TypedValue((int)DxfCode.Start, "LWPOLYLINE") };
                SelectionFilter filter = new SelectionFilter(filList);
                PromptSelectionOptions opts = new PromptSelectionOptions();
                opts.MessageForAdding = "Select polylines: ";
                PromptSelectionResult res = Ced.GetSelection(opts, filter);
                // Do nothing if selection is unsuccessful
                if (res.Status != PromptStatus.OK) 
                {
                    return;
                }
                SelectionSet selSet = res.Value;
                ObjectId[] ids = selSet.GetObjectIds();


                for (int i = 0; i < ids.Length; i++)
                {
                    // Get Data from PolyLine
                    Polyline pl = (Polyline)Ctr.GetObject(ids[i], OpenMode.ForRead);
                    Database newDb = new Database(true, false);
                    string FileName = "C:\\temp\\wblock.dwg";
                    newDb.SaveAs(FileName, DwgVersion.Newest);
                    DrawPline(pl, FileName);
                }
            }

            

        }
        public static void DrawPline(Polyline polyline,string doc)
        {
            try
            {
                string dwgFlpath = doc;
                using (Database db = new Database(false, true))
                {
                    db.ReadDwgFile(dwgFlpath, FileOpenMode.OpenForReadAndAllShare, false, null);
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                        BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                        using (Polyline pl = new Polyline(polyline.NumberOfVertices))
                        {
                            pl.Normal = new Vector3d(0, 0, 1);
                            pl.Elevation = 0;

                            for (int j = 0; j < polyline.NumberOfVertices; j++)
                            {
                                Point2d pt = polyline.GetPoint2dAt(j);

                                double bul = polyline.GetBulgeAt(j);
                                double sWid = polyline.GetStartWidthAt(j);
                                double eWid = polyline.GetEndWidthAt(j);
                                pl.AddVertexAt(j, new Point2d(pt.X, pt.Y), bul, sWid, eWid);
                                
                            }
                            pl.Closed = true;

                            btr.AppendEntity(pl);
                            tr.AddNewlyCreatedDBObject(pl, true);
                        }
                        tr.Commit();
                    }
                    db.SaveAs(dwgFlpath, DwgVersion.Current);
                }
                
            }
            catch (System.Exception ex)
            {
                Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
            }
        }
    }

 

0 Likes
Message 5 of 5

devitg
Advisor
Advisor

It is a LISP forum . 

0 Likes