庭園の歩道の輪郭を描くのに必要なコードのすべてが揃いました。
;;;---------------------------------------------------------------
;;; Function: gp:drawOutline
;;;---------------------------------------------------------------
;;; Description: This function will draw the outline of the garden
;;; path.
;;;---------------------------------------------------------------
;;; Note: No error checking or validation is performed on the
;;; BoundaryData parameter. The sequence of items within this
;;; parameter does not matter, but it is assumed that all sublists
;;; are present and contain valid data.
;;; --------------------------------------------------------------
(defun gp:drawOutline (BoundaryData / VLADataPts PathAngle
Width HalfWidth StartPt PathLength
angm90 angp90 p1 p2
p3 p4 polypoints pline
)
;; extract the values from the list BoundaryData
(setq PathAngle (cdr (assoc 50 BoundaryData))
Width (cdr (assoc 40 BoundaryData))
HalfWidth (/ Width 2.00)
StartPt (cdr (assoc 10 BoundaryData))
PathLength (cdr (assoc 41 BoundaryData))
angp90 (+ PathAngle (Degrees->Radians 90))
angm90 (- PathAngle (Degrees->Radians 90))
p1 (polar StartPt angm90 HalfWidth)
p2 (polar p1 PathAngle PathLength)
p3 (polar p2 angp90 Width)
p4 (polar p3 (+ PathAngle (Degrees->Radians 180)) PathLength)
polypoints (apply 'append
(mapcar '3dPoint->2dPoint (list p1 p2 p3 p4))
)
)
;; ***** data conversion *****
;; Notice, polypoints is in AutoLISP format, consisting of a list
;; of the 4 corner points for the garden path.
;; The variable needs to be converted to a form of input parameter
;; acceptable to ActiveX calls.
(setq VLADataPts (gp:list->variantArray polypoints))
;; Add polyline to the model space using ActiveX automation.
(setq pline (vla-addLightweightPolyline
*ModelSpace*; Global Definition for Model Space
VLADataPts
) ;_ end of vla-addLightweightPolyline
) ;_ end of setq
(vla-put-closed pline T)
;; Return the ActiveX object name for the outline polyline
;; The return value should look something like this:
;; #<VLA-OBJECT IAcadLWPolyline 02351a34>
pline
) ;_ end of defun
これで gp:drawOutline が、この関数の骨格バージョンで使用したクォーテーション付きのシンボル 'SomeEname ではなく、変数 pline を返すようになった点に注意してください。
;;;--------------------------------------------------------------
;;; First step is to load ActiveX functionality. If ActiveX support
;;; already exists in document (can occur when Bonus tools have been
;;; loaded into AutoCAD), nothing happens. Otherwise, ActiveX
;;; support is loaded.
;;;---------------------------------------------------------------
(vl-load-com)
;;; In Lesson 4, the following comment and code is moved to utils.lsp
;;;---------------------------------------------------------------
;;; For ActiveX functions, we need to define a global variable that
;;; "points" to the Model Space portion of the active drawing. This
;;; variable, named *ModelSpace* will be created at load time.
;;;---------------------------------------------------------------
(setq *ModelSpace*
(vla-get-ModelSpace
(vla-get-ActiveDocument (vlax-get-acad-object))
) ;_ end of vla-get-ModelSpace
) ;_ end of setq
なぜ上記のコードを、すべての defun の外側に記述するかを考えてください理由は、ファイルがロードされたときに、それらのコードが自動的に実行されるからです。
(setq PolylineName (gp:drawOutline))
これを、次のように変更します。
(setq PolylineName (gp:drawOutline gp_PathData))
gp:drawOutline 関数はパラメータ(ポリライン境界データが格納されたリスト)が必要になっているので、このように変更することで要求に応えます。
改訂したプログラムをロードして実行してください。Visual LISP は、最終結果が表示される前に、AutoCAD からコントロールを受け取るため、Visual LISP にコントロールが戻った後、AutoCAD ウィンドウに切り替えてください。プログラムが正常に実行された場合は、庭園の歩道の境界が表示されています。エラーが発生した場合は、コードをデバッグして、もう一度実行してください。