Autolisp: OSNAP and dist command

Autolisp: OSNAP and dist command

Anonymous
Not applicable
2,905 Views
28 Replies
Message 1 of 29

Autolisp: OSNAP and dist command

Anonymous
Not applicable

Good morning,

 

I'm back with this weird thing happening.

 

I'm measuring distance from a point on XY and the imprint of a line from that point (200 units tall) with a solid tall 152.40 units.

Strangely in my computer works just fine measuring 152.40 and on other two computers it shows 200.

 

I think OSNAP is not snapping on the solid on theirs, but checked the options and they look the same to me.

 

Can anyone help? Thanks in advance.

 

below is my code:

(defun C:DAVIDE(/ var val p c solidSS ent linevar)

    (setq var '("Osmode" "Expert")
          val  (mapcar 'getvar var))
    (mapcar 'setvar var '(0 1))

; TURN OFF SOLID LAYER
    (command "_.-layer" "OFF" "IMAGINARY SURFACE" "" "")

; ENTER LAT, LON OF THE POINT OF INTEREST
    (command "geomarklatlong" PAUSE PAUSE " ")

; SELECT NODE JUST CREATED
    (setq p (cdr (assoc 10 (entget(ENTLAST)))))

; DRAW LINE 200 UNITS TALL ORTHO TO XY FROM NODE
    (command "_.line" p (list (car p) (cadr p) (+ (caddr p) 200)) "")
    (setq linevar (ssget "_L"))
    (mapcar 'setvar var val)

; TURN ON SOLID LAYER
    (command "_.-layer" "ON" "IMAGINARY SURFACE" "")

; IMPRINT LINE WITH SOLID
    (setq solidSS (ssget "X" (list '(0 . "3DSOLID")(cons 8 "IMAGINARY SURFACE")))) ; this will get a selection set containing the solid
    (setq ent (ssname solidSS 0)) ; retrieve first entity in the list (there should be only 1)
    (command "_.imprint" ent linevar "_Y" "")

; SELECT IMPRINTED POINT
    (setq c (getvar "lastpoint"))

; CALCULATE DISTANCE FROM ORIGINAL POINT TO THE IMPRINTED POINT
    (command "dist" p c)
    (if (> (getvar 'distance) 152.40)
   (alert (strcat "Error"))
 (alert (strcat "Max heigth is :  " (rtos (getvar 'distance) 2 3) " mt  or  " (rtos (* 3.2808399 (getvar 'distance)) 2 3) " ft")))
    (princ)
)

0 Likes
Accepted solutions (1)
2,906 Views
28 Replies
Replies (28)
Message 21 of 29

DannyNL
Advisor
Advisor

Hi Davide,

 

No, not directly to be honest.

I've just tested again with a clean copy of the example drawing you posted and the values. This seems to be working OK.

 

davide.gif

 

So I'm not sure what is different in the drawing you are using it in.

Is it possible to post a copy of your drawing with only the solid in it and the values you are using?

 

 

0 Likes
Message 22 of 29

Anonymous
Not applicable

That is so funny, I just downloaded the drawing I posted to make sure we are working on the same file and I'm still getting zero distance.

 

Can you please send me or post your lisp file?

0 Likes
Message 23 of 29

DannyNL
Advisor
Advisor

See attached LISP file.

 

What AutoCAD version are you working in?

I've created and tested the LISP in 2017, although that shouldn't really matter but you never know.

0 Likes
Message 24 of 29

Anonymous
Not applicable

It doesn't work for me 😞

 

I'm using AutoCAD 2016

0 Likes
Message 25 of 29

DannyNL
Advisor
Advisor

I'm lost. I've just tried in 2016 and everything seems to be working fine there as well.

Do you get any error messages? Does it maybe work on one of the systems of your colleague?

0 Likes
Message 26 of 29

Anonymous
Not applicable

Thanks Danny,

 

your solution worked on all computers we have at work (except mine).

If you have any idea why mine shows zero as distance it would be greatly appreciated.

 

Can i do some kind of Autocad "factory reset"? I can't remove it and reinstall it.

0 Likes
Message 27 of 29

DannyNL
Advisor
Advisor

Hi Davide,

 

That is partly good news but not so for you PC. It's hard to tell what is causing the problems without access to the PC itself.

 

I don't know if you are using customized profiles in AutoCAD, but you might want to try a copy or a reset on your current AutoCAD profile and see if it solves the problem. To test with another profile, just go to Options-->Profiles and Add a new profile. Activate that profile, optionally reset the new profile to be sure no funny settings are kept and try again. If the routine then works on your PC, it's probably a profile/variable issue.

Another thing you can try if you and your colleagues all work with the same settings, is to export the current profile on one of the other PC's and import that one on your own PC and see if that solves the issue.

Message 28 of 29

Anonymous
Not applicable

I reset my profile and it worked!

 

thanks again for your time and patience.

0 Likes
Message 29 of 29

DannyNL
Advisor
Advisor

That's great news.

Glad I could help Smiley Happy

 

I've received some suggestions for some small changes from @hmsilva to optimize a part of the code and to set and use the WCS for calculations.

Please see the code from Henrique below and credits to him for these modifications.

 

 

(defun C:DAVIDE (/ height lastobj p ref solidss val var)
    (setq var '("Osmode" "Expert")
          val (mapcar 'getvar var)
    )
    (mapcar 'setvar var '(0 1))
 ; set UCS World
    (command "_.ucs" "_W")
 ; ENTER LAT, LON OF THE POINT OF INTEREST
    (command "geomarklatlong" PAUSE PAUSE " ")
 ; SELECT NODE JUST CREATED
    (setq p (cdr (assoc 10 (entget (ENTLAST)))))
 ; DRAW REFERENCE CYLINDER WIT BASE HEIGHT
    (command "_cylinder" p 0.001 500)
    (setq ref (entlast))
 ; RETRIEVE SOLID
    (setq solidSS (ssget "X" (list '(0 . "3DSOLID") (cons 8 "IMAGINARY SURFACE")))) ; this will get a selection set containing the solid
 ; SUBTRACT COPIED SOLID FROM REFERENCE CYLINDER
    (command "_.-interfere" ref "" (ssname solidSS 0) "" "_Y")
 ; Set lastobj with the las object
    (setq lastobj (entlast))
 ; test is there is a new object in database
    (if (not (eq ref lastobj))
        (progn
 ; GET HEIGHT OF new CYLINDER from INTERFERE...
            (vla-GetBoundingBox (vlax-ename->vla-object lastobj) 'BoxPoint1 'BoxPoint2)
            (setq height (nth 2 (vlax-safearray->list BoxPoint2)))
 ; ERASE REFERENCE CYLINDER  and  
            (command "_erase" ref lastobj "")
 ; DISPLAY MEASURED HEIGHT OR ERROR IF > 152.4   
            (if (> height 152.40)
                (alert (strcat "Error"))
                (alert
                    (strcat "Max heigth is :  " (rtos height 2 3) " mt  or  " (rtos (* 3.2808399 height) 2 3) " ft")
                )
            )
        )
        (progn
            (command "_erase" ref "")
            (alert (strcat "LAT and LON don't interfere..."))
        )
    )
 ; set UCS previous
    (command "_.ucs" "_P")
    (mapcar 'setvar var val)
    (princ)
)