eg0n 发表于 2022-7-6 10:14:03

阵列维度

我想用列表中的元素填充数组。问题是,当我想定义数组的维数时,上界取决于列表的长度,因此我使用以下代码:
 
(setq数组(vlax make safearray vlax vbDouble’(0(-(length newlist)1)))
 
我得到了一个“错误的参数类型:fixnump:((LENGTH NEWLIST)1)”错误。
 
有什么想法真的很有帮助吗?

Tharwat 发表于 2022-7-6 10:27:41

既然您已经开始编写所需例程的一些代码,那么最好将它们全部发布以查看它们,或者可以添加我们的代码供您使用。
 
谢谢
 
塔瓦特

eg0n 发表于 2022-7-6 10:35:48

我想做的是通过删除不重要的点来简化多段线。作为“不重要”,我定义了角度内的每个点。在下图中,点3不重要(内角),但点5很重要。
 
http://img827.imageshack.us/img827/5728/exampleh.jpg
 
;(prompt "\nType \"mine\" to run........")

(defun c:mine ( / theobj thelist n xval1 yval1 newlist maxdeg amax xval2 yval2 xval3 yval3 d1 d2 d3 z a l anarray mspace myobj)
                           
;load the visual lisp extensions
(vl-load-com)
                           
;get the entity and entity name
(setq theobj (car (entsel "\nSelect a Polyline: ")))
                           
;convert to vl object
(setq theobj (vlax-ename->vla-object theobj))

;retrieve the coordinates
(setq thelist (vlax-get-property theobj 'coordinates))

;convert to a list
(setq thelist (vlax-safearray->list(variant-value thelist)))

;zero the counter
(setq n 0)

;make a list of the simplified coordinates
(setq xval1 (car thelist))
(setq yval1 (cadr thelist))
(setq newlist '(xval1 yval1))

;ask for maximum angle
(setq maxdeg (getint "\nEnter Maximum Angle (0 - 90 deg) : "))

;convert degrees to radians using "dtr" routine
(setq amax (dtr maxdeg))

;start the loop and repeat it for (n-2) times
(repeat (- (/ (length thelist) 2) 2)

;get the x coordinates
(setq xval1 (nth n thelist))
(setq xval2 (nth (+ 2 n) thelist))
(setq xval3 (nth (+ 4 n) thelist))

;increase the counter
(setq n (1+ n))

;get the y coordinates
(setq yval1 (nth n thelist))
(setq yval2 (nth (+ 2 n) thelist))
(setq yval3 (nth (+ 4 n) thelist))

;calculate distances using "distan" routine
(setq d1 (distan xval1 xval2 yval1 yval2))
(setq d2 (distan xval1 xval3 yval1 yval3))
(setq d3 (distan xval2 xval3 yval2 yval3))

;calculate angle
(setq z (/ (- (+ (expt d1 2) (expt d2 2)) (expt d3 2)) (* 2 (* d1 d2))))   
(setq a (arccos z))

;compare the angles
(if (> a (/ amax 2))

;if true do the following
(progn

;add the coordinates to the new list
(setq newlist (append '(newlist xval3 yval3)))

);progn

);if

;increase the counter
(setq n (1+ n))

);repeat

;add the last point to the simplified list
(setq xval1 (nth (- (length thelist) 1) thelist))
(setq yval1 (last thelist))
(setq newlist (append '(newlist xval1 yval1)))

;make an array with the points of the simplified list
(setq anarray (vlax-make-safearray vlax-vbDouble '(0 . (- (length newlist) 1))))

;fill it with x and y values
(vlax-safearray-fill anarray newlist)

;reference to model space
(setq mspace (vla-get-modelSpace (vla-get-activeDocument (vlax-get-acad-object))))

;draw the simplified polyline
(setq myobj (vla-addLightweightPolyline mspace anarray))

(princ)

);defun

;------------------------------------------
;This function converts Degrees to Radians.
(defun dtr (x)
;define degrees to radians function
(* pi (/ x 180.0))
;divide the angle by 180 then
;multiply the result by the constant PI
) ;end of function

;clean loading
(princ)

;This function calculates distance
(defun distan (x1 x2 y1 y2)

;calculate dx dy
(setq pt1 (list x1 y1))
(setq pt2 (list x2 y2))

;find the distance
(distance pt1 pt2)

) ;end of function

;clean loading
(princ)

;This function inverse cosine (ArcCos)
;Args: -1 <= x <= 1

(defun arccos (x)
(atan (/ (sqrt (- 1 (expt x 2))) x))
)

;clean loading
(princ)

;-------------------------------------------


;End of mine.lsp
;---------------------------

Lee Mac 发表于 2022-7-6 10:46:46

我做了李建议的改变,没关系(谢谢李!!!)但现在在下一行代码:
 
(vlax safearray fill anarray newlist)
 
我得到一个“lisp值不强制使用这种类型的变量:NEWLIST”错误。
这是什么意思?

eg0n 发表于 2022-7-6 10:59:01

看看你是如何构建“新列表”的-你确定你读了我的链接。。。

Lee Mac 发表于 2022-7-6 11:13:00

为我的第一个Lisp程序感到自豪。谢谢李的帮助和你的链接(我第二次读了,纠正了很多错误…)。

eg0n 发表于 2022-7-6 11:23:53

页: [1]
查看完整版本: 阵列维度