Author Topic: Can someone take a look at my code.  (Read 1918 times)

0 Members and 1 Guest are viewing this topic.

dubb

  • Swamp Rat
  • Posts: 1105
Can someone take a look at my code.
« on: December 29, 2016, 03:33:43 PM »
The purpose of this script is to insert an attribute block and increment the number. I know it can be written better. Not asking to rewrite the whole thing but I am having an error at the end of the routine. It also runs very slow because maybe I am using the
Code: [Select]
Command function to edit attributes and other practices I am not aware of.

Code: [Select]
(defun c:as(/ divlen numdiv len obj startpoint num newlen)
  (setvar "cmdecho" 0)
(defun round ( n ) (fix (+ n (if (minusp n) -0.5 0.5)))) ;;round a number
  (setq obj (vlax-ename->vla-object (car (entsel "\n >> Select profile >>"))))
(setq len (vlax-curve-getdistatpoint obj (vlax-curve-getendpoint obj)))
(setq divlen 100.00)
  (setq numdiv (round (/ len divlen)))
(setq startpoint (vlax-curve-getendPoint obj))
  (setq num (getreal "\nEnter start number :"))
  (initget "+ -")
  (setq inc (getkword "\nEnter (-) decrement or (+) Increment :")
)
  ;(initget "R N")
    (setq rot (getstring "\nEnter (R) Reverse Press Enter to ignore:")
)
     ;(setq num (1- num))

(if (= divlen len)
  (prompt "\nDivsion length is greater than the line's length")
  (progn
    ;(prompt "\nDivsion length is shorter than line's length")
    (setq newlen 0)
    (repeat numdiv
      (setq newlen (+ divlen newlen))
      (setq pt (vlax-curve-getpointatdist obj newlen))
      ;(princ loopCnt)
              (setq ang (angle '(0 0 0)
      (vlax-curve-getfirstderiv obj
(vlax-curve-getparamatpoint obj pt))))
      (if (= rot "r")
(setq ang (+ 3.14159 ang));add 180 degree rotation
)
      (vlax-invoke (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object)))'InsertBlock pt "sta" 1 1 1 ang);***
      (cond
((= inc "+")
(setq num (1+ num))
)
((= inc "-")
  (setq num (1- num))
)
)
      (setq num (rtos num 2 2))
(COMMAND "._ATTEDIT" "Y" "STA" "STA" "STA" "last" "V" "R" num "" "")
      ;(strcat "pt" loopCnt)
      (setq num (atoi num))
      )
    )
  )
  ;(setq pt (vlax-curve-getpointatdist obj 100.00))
  (princ)
  (setvar "cmdecho" 1)
  )

CLI Results
Code: [Select]
Command: as

 >> Select profile >>
Enter start number :100

Enter (-) decrement or (+) Increment :+

Enter (R) Reverse Press Enter to ignore:
1 found
1 found
1 found
1 found
; error: bad argument type: 2D/3D point: nil

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Can someone take a look at my code.
« Reply #1 on: December 29, 2016, 03:39:22 PM »
On my tiny phone so too difficult to do a review.

That said, "in before John snaps because of the thread title". :-D
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Can someone take a look at my code.
« Reply #2 on: December 29, 2016, 03:47:35 PM »
...
That said, "in before John snaps because of the thread title". :-D
*face-palm*

dubb, I looked at you code.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Can someone take a look at my code.
« Reply #3 on: December 29, 2016, 03:51:51 PM »
Maybe set this to:
Code: [Select]
(setq blkRef (vlax-invoke (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object)))'InsertBlock pt "sta" 1 1 1 ang))And then:
Code: [Select]
(vl-some
  (function
    (lambda (x)
      (if (= "STA" (vla-get-TagString x))
        (progn (vla-put-TextString x num) T)
      )
    )
  )
  (vlax-invoke blkRef 'GetAttributes)
)
instead of
Code: [Select]
(COMMAND "._ATTEDIT" "Y" "STA" "STA" "STA" "last" "V" "R" num "" "")
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Can someone take a look at my code.
« Reply #4 on: December 29, 2016, 03:52:50 PM »
I have not tested the code, but just from reading I think you should calculate numdiv differently.
You should use fix instead of round:
Code: [Select]
(setq numdiv (round (/ len divlen)))If numdiv is rounded up the final value of newlen will be bigger than the length of the curve. And this:
Code: [Select]
(vlax-curve-getpointatdist obj newlen)will then return nil.

dubb

  • Swamp Rat
  • Posts: 1105
Re: Can someone take a look at my code.
« Reply #5 on: December 29, 2016, 05:15:52 PM »
...
That said, "in before John snaps because of the thread title". :-D
*face-palm*

dubb, I looked at you code.

 :2funny:
Maybe set this to:
Code: [Select]
(setq blkRef (vlax-invoke (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object)))'InsertBlock pt "sta" 1 1 1 ang))And then:
Code: [Select]
(vl-some
  (function
    (lambda (x)
      (if (= "STA" (vla-get-TagString x))
        (progn (vla-put-TextString x num) T)
      )
    )
  )
  (vlax-invoke blkRef 'GetAttributes)
)
instead of
Code: [Select]
(COMMAND "._ATTEDIT" "Y" "STA" "STA" "STA" "last" "V" "R" num "" "")
Ahh thats what I need to do. I haven't really learned much lambda and mapcar. Thanks for the suggestions.

dubb

  • Swamp Rat
  • Posts: 1105
Re: Can someone take a look at my code.
« Reply #6 on: December 29, 2016, 07:50:43 PM »
Great suggestion, I removed the (round) function as it wasn't needed.
Code: [Select]
(setq divlen 100.00)
(setq numdiv (fix ( / len divlen)))

I have not tested the code, but just from reading I think you should calculate numdiv differently.
You should use fix instead of round:
Code: [Select]
(setq numdiv (round (/ len divlen)))If numdiv is rounded up the final value of newlen will be bigger than the length of the curve. And this:
Code: [Select]
(vlax-curve-getpointatdist obj newlen)will then return nil.