Author Topic: Can anyone help me convert this autolisp to C#?  (Read 1375 times)

0 Members and 1 Guest are viewing this topic.

JohnSnow

  • Newt
  • Posts: 52
Can anyone help me convert this autolisp to C#?
« on: July 24, 2023, 09:54:18 AM »
I have spent quite a long time trying to convert the following code (provide by gile from another website) to C# with no luck so far.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:RotateToView (/ n ss ent elst)
  2.  (if (setq norm (trans '(0. 0. 1.) 2 0 T)
  3.        n     -1
  4.        ss     (ssget '((0 . "POINT,TEXT")))
  5.      )
  6.    (while (setq ent (ssname ss (setq n (1+ n))))
  7.      (setq elst (entget ent)
  8.        ins     (assoc 10 elst)
  9.      )
  10.      (if (= (cdr (assoc 0 elst)) "TEXT")
  11.    (entmod    (subst (cons 10 (trans (cdr ins) ent norm))
  12.               ins
  13.               (subst (cons 210 norm) (assoc 210 elst) elst)
  14.        )
  15.    )
  16.    (entmod (subst (cons 210 norm) (assoc 210 elst) elst))
  17.      )
  18.    )
  19.  )
  20.  (princ)
  21. )

edit-kdub:add code tags
« Last Edit: July 25, 2023, 05:23:53 PM by kdub_nz »

JohnSnow

  • Newt
  • Posts: 52
Re: Can anyone help me convert this autolisp to C#?
« Reply #1 on: July 24, 2023, 09:55:20 AM »
It is to roate the text to the current view.
So I have done something like this: (Bascially set the text normal and position to suit)
But it doesn't work.

Code - C#: [Select]
  1.             Document doc = Application.DocumentManager.MdiActiveDocument;
  2.             Database db = doc.Database;
  3.             Editor ed = doc.Editor;
  4.  
  5.             using (Transaction tr = db.TransactionManager.StartTransaction())
  6.            {
  7.                 BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  8.                 BlockTableRecord modelSpace = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  9.                 ViewTableRecord CurrentView = ed.GetCurrentView();
  10.  
  11.                 var DCS2WCS =   Matrix3d.Rotation(-CurrentView.ViewTwist, CurrentView.ViewDirection, CurrentView.Target) *
  12.                                            Matrix3d.Displacement(CurrentView.Target - Point3d.Origin) *
  13.                                            Matrix3d.PlaneToWorld(CurrentView.ViewDirection);
  14.  
  15.                 Vector3d norm = new Vector3d(0, 0, 1).TransformBy(DCS2WCS);
  16.                Matrix3d WCS2OCS = Matrix3d.WorldToPlane(norm);
  17. ....
  18.                 text.Normal = norm;
  19.                 text.Position = text.Position.TransformBy(WCS2OCS);
  20.  
  21. .....
  22.  
  23.  

edit-kdub:add code tags
« Last Edit: July 25, 2023, 07:21:42 PM by kdub_nz »

HOSNEYALAA

  • Newt
  • Posts: 103
Re: Can anyone help me convert this autolisp to C#?
« Reply #2 on: July 25, 2023, 12:56:23 AM »


Hi
Can you attached example drawing

JohnSnow

  • Newt
  • Posts: 52
Re: Can anyone help me convert this autolisp to C#?
« Reply #3 on: July 25, 2023, 05:26:44 AM »
I have have any drawings in particular. It can be any drawings.

What I am struggling with is to convert the following to C#:

Code - Auto/Visual Lisp: [Select]
  1. (trans (cdr ins) ent norm)


edit-kdub : code tags added
« Last Edit: July 25, 2023, 04:35:31 PM by kdub_nz »

JohnK

  • Administrator
  • Seagull
  • Posts: 10640
Re: Can anyone help me convert this autolisp to C#?
« Reply #4 on: July 25, 2023, 12:27:41 PM »
I have have any drawings in particular. It can be any drawings.

What I am struggling with is to convert the following to C#:
(trans (cdr ins) ent norm)
I haven't looked at any of the code yet but let me just put this out there (so I can possibly look like a complete idiot).
TRANS: https://www.theswamp.org/Sources/doc/avlisp/#trans
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: Can anyone help me convert this autolisp to C#?
« Reply #5 on: July 25, 2023, 07:10:56 PM »
It is to rotate the text to the current view.

But it doesn't work.
Why do you think this? It appears to work for me. You didn't show your whole code so I can't speculate what may be causing it to not work for you.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8702
  • AKA Daniel
Re: Can anyone help me convert this autolisp to C#?
« Reply #6 on: July 26, 2023, 06:46:24 AM »
there's aWcs2Dcs here, maybe just get the inverse
 https://adndevblog.typepad.com/autocad/2012/06/setting-the-view-center-in-an-external-dwg-file-with-objectarx-or-realdwg-in-c.html

actually the sample uses inverse, so just remove that line... maybe...

BIGAL

  • Swamp Rat
  • Posts: 1414
  • 40 + years of using Autocad
Re: Can anyone help me convert this autolisp to C#?
« Reply #7 on: July 26, 2023, 09:05:28 PM »
If you have an old PC in the closest, try LISP2C its 8bit software made years ago to convert lsp to C.

Yes have a copy but does not run on 64 Bit pc.
A man who never made a mistake never made anything