;;; ------------------------------------------------------------------------
;;; CreateDiffuser.lsp v1.1
;;;
;;; Copyright © January, 2007
;;; Timothy G. Spangler
;;;
;;; Permission to use, copy, modify, and distribute this software
;;; for any purpose and without fee is hereby granted, provided
;;; that the above copyright notice appears in all copies and
;;; that both that copyright notice and the limited warranty and
;;; restricted rights notice below appear in all supporting
;;; documentation.
;;;
;;; THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED
;;; WARRANTY. ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR
;;; PURPOSE AND OF MERCHANTIBILITY ARE HEREBY DISCLAIMED BY THE
;;; PROGRAMMER.
;;;
;;; -----------------------------------------------------------------------

;;; ------------ COMMAND LINE FUNCTIONS
(defun c:DIFF (/) (DIFF_START))

;;; ------------ MAIN FUNCTION
(defun DIFF_START ( / *error* Angle+90 Angle-90 ActiveDoc TransAlign DuctAgnle DuctHeight DuctWidth DuctStyle TransType OldClayer OldCmdEcho Space)

;;; Begin Error Handler -------------------------------------------------
(defun *error* (MSG)

(if (not (member MSG '("Function cancelled" "quit / exit abort")))
(princ (strcat "\n*** Program Error: " (strcase MSG) " ***"))
(princ "\n... Program Cancelled ...")
)
(while (< 0 (getvar "cmdactive"))
(command)
)
(princ)
)
;;; End Error Handler ---------------------------------------------------
(DIFF_SET_ENV)
)
;;; ------------ GET VARIABLES FOR DIFFUSER CREATION
(defun DIFF_RUN (/)

(initget "Supply s S Return r R Exhaust e E")
(setq DiffStyle (getkword "Enter diffuser style: (Supply/Return/Exhaust)"))

(initget "Lay-In l L Mounted m M")
(setq DiffType (getkword "Enter diffuser type: (Lay-In/Mounted)"))

(initget "Square s S Round r R")
(setq DiffShape (getkword "Enter diffuser Shape: (Square/Round)"))

(if (= (strcase DiffShape) "SQUARE")
(progn
(setq DiffWidth (getreal "\n Enter diffuser width: (12\")"))
(if (not DiffWidth)
(setq DiffWidth 12.0)
)
(if (< DiffWidth 12)
(setq DiffWidth (getreal "\n Enter diffuser width: \n -Must be greater than 12\"- "))
)
(setq DiffLength (getreal "\n Enter diffuser length: (12\")"))
(if (not DiffLength)
(setq DiffLength 12.0)
)
(if (< DiffLength 12)
(setq DiffLength (getreal "\n Enter diffuser length: \n -Must be greater than 12\"- "))
)
)
(progn
(setq DiffWidth (getreal "\n Enter diffuser diameter: (12\")"))
(if (not DiffWidth)
(setq DiffWidth 12.0)
)
(if (< DiffWidth 12)
(setq DiffWidth (getreal "\n Enter diffuser diameter: \n -Must be greater than 12\"- "))
)
)
)

(if(not(setq DiffInlet (getreal "Enter diffuser inlet size: (6\")")))
(setq DiffInlet 6.0)
)

(while(null(setq InsPoint (getpoint " Define insertion point: ")))
(princ "-Point not defined-")
)
(cond
((= (strcase DiffType) "LAY-IN")
(if(= (strcase DiffShape) "SQUARE")
(DIFF_LAYIN_SQUARE InsPoint DiffWidth DiffLength DiffInlet)
(DIFF_LAYIN_ROUND InsPoint DiffWidth DiffInlet)
)
)
((= (strcase DiffType) "MOUNTED")
(if(= (strcase DiffShape) "SQUARE")
(DIFF_MOUNTED_SQUARE InsPoint DiffWidth DiffLength DiffInlet)
(DIFF_MOUNTED_ROUND InsPoint DiffWidth DiffInlet)
)
)
)
(princ)
)
;;; ------------ LAYER CREATION ROUINE
(defun DIFF_CREATE_LAYER (Layer Descpition Linetype Thickness Color Plot / TmpList VLA-Obj)

;; Check to see if linetype exsists
(if (= (tblsearch "ltype" Linetype) nil)
(if (DIFF_CHECK_LINETYPE (findfile "acad.lin") Linetype)
(command "linetype" "load" Linetype "acad.lin" "")
(setq Linetype "Continuous")
)
)
;;; ------------ CREATE A LIST FOR ENTMAKE
(setq TmpList
(list
(cons 0 "LAYER")
(cons 100 "AcDbSymbolTableRecord")
(cons 100 "AcDbLayerTableRecord")
(cons 70 0)
)
)
;; Create layer name list
(setq TmpList (append TmpList (list (cons 2 Layer))))
;; Create layer color list
(setq TmpList (append TmpList (list (cons 62 (atoi Color)))))
;; Create layer linetype list
(setq TmpList (append TmpList (list (cons 6 Linetype))))
;; Create layer lineweight list
(setq TmpList (append TmpList (list (cons 370 (atoi Thickness)))))
;; Create layer plot list
(setq TmpList (append TmpList (list (cons 290 (atoi Plot)))))
;; Create layer from first item in the list
(entmake TmpList)
;; Create layer description
(if(or(= 16.1 (atof(getvar "acadver")))(< 16.1 (atof(getvar "acadver"))))
(progn
(setq VLA-Obj(vla-Add (vla-Get-Layers ActiveDoc)Layer))
(vla-Put-Description VLA-Obj Descpition)
(vlax-release-object VLA-Obj)
)
)
)
;;; ------------ CHECKS TO SEE IF A LINETYPE IS AVAILIBLE
(defun DIFF_CHECK_LINETYPE (LINFile Linetype / OpenFile LineNumber CurrentLine Result)

(setq OpenFile (open LINFile "r"))
(while (setq CurrentLine (read-line OpenFile))
(if (wcmatch CurrentLine "`**")
(progn
(setq LinetypeName (substr(car(DIFF_STRING_TO_LIST CurrentLine ","))2))
(if (= (strcase Linetype) LinetypeName)
(setq Result T)
)
)
)
)
(close OpenFile)
Result
)
;;; ------------ STRING TO LIST SUB ROUTINE
(defun DIFF_STRING_TO_LIST (Stg Del / CurChr PosCnt TmpLst TmpStr)

(setq PosCnt 1
TmpStr ""
)
(repeat (1+ (strlen Stg))
(setq CurChr (substr Stg PosCnt 1))
(if (= CurChr Del)
(progn
(setq TmpLst (cons TmpStr TmpLst))
(setq TmpStr "")
)
(setq TmpStr (strcat TmpStr CurChr))
)
(setq PosCnt (1+ PosCnt))
)
(setq TmpLst (reverse TmpLst))
)
;;; ------------ CREATE LAY-IN SQUARE DIFFUSER
(defun DIFF_LAYIN_SQUARE(InsPoint DiffWidth DiffLength DiffInlet /
DiffPointList DiffPoints ExhsPointList RtrnPointList Stop Counter TempPoint vlaDiffuser Ang StopPoint VlaLine VlaCircle)

(cond
((= (strcase DiffStyle)"SUPPLY")
;; Create the layer for the diffuser
(DIFF_CREATE_LAYER "M-DIFF-SUPP" "Mechanical Plan - Supply diffusers, register and grilles" "Continuous" "35" "133" "1")

;; Get the outside points from the insertion point
(setq DiffPointList(DIFF_GET_POINTS InsPoint DiffWidth DiffLength))

;; Create list of 2D point from DiffPointList
(setq Stop (length DiffPointList))
(setq Counter 0)
(while (/= Counter Stop)
(setq TempPoint(reverse(cdr(reverse (nth Counter DiffPointList)))))
(setq DiffPoints (append TempPoint DiffPoints))
(setq Counter (1+ Counter))
)

;; Draw the outside of the diffuser from the 2D points
(setq vlaDiffuser (vla-addLightweightPolyline Space
(vlax-safearray-fill
(vlax-make-safearray vlax-vbDouble (cons 0 (- (length DiffPoints) 1)))
DiffPoints))
)
;; Close the polyline
(vla-put-closed vlaDiffuser :vlax-true)

;; Offset the polyline for the layin edge
(setq DiffLayIn (car(vlax-invoke vlaDiffuser 'Offset 1)))

;; Draw line to show it is a supply diffuser
(foreach X DiffPointList
(setq Ang (angle InsPoint X))
(setq StopPoint (polar X Ang (-(/ DiffInlet 2)(distance InsPoint X))))
(setq VlaLine (vlax-invoke space 'AddLine X StopPoint))
;; Set properties
(vlax-put VlaLine 'Layer "M-DIFF-SUPP")
)
;; Draw the center inlet
(setq VlaCircle (vlax-invoke space 'AddCircle InsPoint (/ DiffInlet 2)))

;; Set the properties on the newly created objects
(vlax-put vlaDiffuser 'Layer "M-DIFF-SUPP")
(vlax-put VlaCircle 'Layer "M-DIFF-SUPP")
(vlax-put DiffLayIn 'Layer "M-DIFF-SUPP")
(vlax-put DiffLayIn 'Color "8")

;; Silent exit
(princ)
)
((= (strcase DiffStyle)"RETURN")
;; Create the layer for the diffuser
(DIFF_CREATE_LAYER "M-DIFF-RETN" "Mechanical Plan - Return diffusers, register and grilles" "Continuous" "35" "23" "1")

;; Get the outside points from the insertion point
(setq DiffPointList(DIFF_GET_POINTS InsPoint DiffWidth DiffLength))
(setq RtrnPointList (list (nth 0 DiffPointList)(nth 2 DiffPointList)))

;; Create list of 2D point from DiffPointList
(setq Stop (length DiffPointList))
(setq Counter 0)
(while (/= Counter Stop)
(setq TempPoint(reverse(cdr(reverse (nth Counter DiffPointList)))))
(setq DiffPoints (append TempPoint DiffPoints))
(setq Counter (1+ Counter))
)

;; Draw the outside of the diffuser from the 2D points
(setq vlaDiffuser (vla-addLightweightPolyline Space
(vlax-safearray-fill
(vlax-make-safearray vlax-vbDouble (cons 0 (- (length DiffPoints) 1)))
DiffPoints))
)
;; Close the polyline
(vla-put-closed vlaDiffuser :vlax-true)

;; Offset the polyline for the layin edge
(setq DiffLayIn (car(vlax-invoke vlaDiffuser 'Offset 1)))

;; Draw line to show it is a return diffuser
(foreach X RtrnPointList
(setq Ang (angle InsPoint X))
(setq StopPoint (polar X Ang (-(/ DiffInlet 2)(distance InsPoint X))))
(setq VlaLine (vlax-invoke space 'AddLine X StopPoint))
;; Set properties
(vlax-put VlaLine 'Layer "M-DIFF-RETN")
)
;; Draw the center inlet
(setq VlaCircle (vlax-invoke space 'AddCircle InsPoint (/ DiffInlet 2)))

;; Set the properties on the newly created objects
(vlax-put vlaDiffuser 'Layer "M-DIFF-RETN")
(vlax-put VlaCircle 'Layer "M-DIFF-RETN")
(vlax-put DiffLayIn 'Layer "M-DIFF-RETN")
(vlax-put DiffLayIn 'Color "8")

;; Silent exit
(princ)
)
((= (strcase DiffStyle)"EXHAUST")
;; Create the layer for the diffuser
(DIFF_CREATE_LAYER "M-DIFF-EXHS" "Mechanical Plan - Exhaust diffusers, register and grilles" "Continuous" "35" "83" "1")

;; Get the outside points from the insertion point
(setq DiffPointList(DIFF_GET_POINTS InsPoint DiffWidth DiffLength))
(setq ExhsPointList (list (nth 0 DiffPointList)(nth 1 DiffPointList)(nth 2 DiffPointList)))

;; Create list of 2D point from DiffPointList
(setq Stop (length DiffPointList))
(setq Counter 0)
(while (/= Counter Stop)
(setq TempPoint(reverse(cdr(reverse (nth Counter DiffPointList)))))
(setq DiffPoints (append TempPoint DiffPoints))
(setq Counter (1+ Counter))
)

;; Draw the outside of the diffuser from the 2D points
(setq vlaDiffuser (vla-addLightweightPolyline Space
(vlax-safearray-fill
(vlax-make-safearray vlax-vbDouble (cons 0 (- (length DiffPoints) 1)))
DiffPoints))
)
;; Close the polyline
(vla-put-closed vlaDiffuser :vlax-true)

;; Offset the polyline for the layin edge
(setq DiffLayIn (car(vlax-invoke vlaDiffuser 'Offset 1)))

;; Draw line to show it is a supply diffuser
(foreach X ExhsPointList
(setq Ang (angle InsPoint X))
(setq StopPoint (polar X Ang (-(/ DiffInlet 2)(distance InsPoint X))))
(setq VlaLine (vlax-invoke space 'AddLine X StopPoint))
;; Set properties
(vlax-put VlaLine 'Layer "M-DIFF-EXHS")
)
;; Draw the center inlet
(setq VlaCircle (vlax-invoke space 'AddCircle InsPoint (/ DiffInlet 2)))

;; Set the properties on the newly created objects
(vlax-put vlaDiffuser 'Layer "M-DIFF-EXHS")
(vlax-put VlaCircle 'Layer "M-DIFF-EXHS")
(vlax-put DiffLayIn 'Layer "M-DIFF-EXHS")
(vlax-put DiffLayIn 'Color "8")

;; Silent exit
(princ)
)
)
(DIFF_RESET_ENV)
)
;;; ------------ CREATE LAY-IN SQUARE ROUND
(defun DIFF_LAYIN_ROUND(InsPoint DiffWidth DiffInlet /
DiffPointList DiffPoints ExhsPointList RtrnPointList Stop Counter TempPoint vlaDiffuser Ang StopPoint VlaLine VlaCircle)

(cond
((= (strcase DiffStyle)"SUPPLY")
;; Create the layer for the diffuser
(DIFF_CREATE_LAYER "M-DIFF-SUPP" "Mechanical Plan - Supply diffusers, register and grilles" "Continuous" "35" "133" "1")

;; Get the outside points from the insertion point
(setq DiffPointList(DIFF_GET_POINTS InsPoint (DIFF_INSCRIBE_SQUARE DiffWidth)(DIFF_INSCRIBE_SQUARE DiffWidth)))

;; Create list of 2D point from DiffPointList
(setq Stop (length DiffPointList))
(setq Counter 0)
(while (/= Counter Stop)
(setq TempPoint(reverse(cdr(reverse (nth Counter DiffPointList)))))
(setq DiffPoints (append TempPoint DiffPoints))
(setq Counter (1+ Counter))
)

;; Draw the center inlet
(setq vlaDiffuser (vlax-invoke space 'AddCircle InsPoint (/ DiffWidth 2)))

;; Offset the polyline for the layin edge
(setq DiffLayIn (car(vlax-invoke vlaDiffuser 'Offset -1)))

;; Draw line to show it is a supply diffuser
(foreach X DiffPointList
(setq Ang (angle InsPoint X))
(setq StopPoint (polar X Ang (-(/ DiffInlet 2)(distance InsPoint X))))
(setq VlaLine (vlax-invoke space 'AddLine X StopPoint))
;; Set properties
(vlax-put VlaLine 'Layer "M-DIFF-SUPP")
)
;; Draw the center inlet
(setq VlaCircle (vlax-invoke space 'AddCircle InsPoint (/ DiffInlet 2)))

;; Set the properties on the newly created objects
(vlax-put vlaDiffuser 'Layer "M-DIFF-SUPP")
(vlax-put VlaCircle 'Layer "M-DIFF-SUPP")
(vlax-put DiffLayIn 'Layer "M-DIFF-SUPP")
(vlax-put DiffLayIn 'Color "8")

;; Silent exit
(princ)
)
((= (strcase DiffStyle)"RETURN")
;; Create the layer for the diffuser
(DIFF_CREATE_LAYER "M-DIFF-RETN" "Mechanical Plan - Return diffusers, register and grilles" "Continuous" "35" "23" "1")

;; Get the outside points from the insertion point
(setq DiffPointList(DIFF_GET_POINTS InsPoint (DIFF_INSCRIBE_SQUARE DiffWidth)(DIFF_INSCRIBE_SQUARE DiffWidth)))
(setq RtrnPointList (list (nth 0 DiffPointList)(nth 2 DiffPointList)))

;; Create list of 2D point from DiffPointList
(setq Stop (length DiffPointList))
(setq Counter 0)
(while (/= Counter Stop)
(setq TempPoint(reverse(cdr(reverse (nth Counter DiffPointList)))))
(setq DiffPoints (append TempPoint DiffPoints))
(setq Counter (1+ Counter))
)

;; Draw the outside of the diffuser from the 2D points
(setq vlaDiffuser (vlax-invoke space 'AddCircle InsPoint (/ DiffWidth 2)))

;; Offset the polyline for the layin edge
(setq DiffLayIn (car(vlax-invoke vlaDiffuser 'Offset -1)))

;; Draw line to show it is a return diffuser
(foreach X RtrnPointList
(setq Ang (angle InsPoint X))
(setq StopPoint (polar X Ang (-(/ DiffInlet 2)(distance InsPoint X))))
(setq VlaLine (vlax-invoke space 'AddLine X StopPoint))
;; Set properties
(vlax-put VlaLine 'Layer "M-DIFF-RETN")
)
;; Draw the center inlet
(setq VlaCircle (vlax-invoke space 'AddCircle InsPoint (/ DiffInlet 2)))

;; Set the properties on the newly created objects
(vlax-put vlaDiffuser 'Layer "M-DIFF-RETN")
(vlax-put VlaCircle 'Layer "M-DIFF-RETN")
(vlax-put DiffLayIn 'Layer "M-DIFF-RETN")
(vlax-put DiffLayIn 'Color "8")

;; Silent exit
(princ)
)
((= (strcase DiffStyle)"EXHAUST")
;; Create the layer for the diffuser
(DIFF_CREATE_LAYER "M-DIFF-EXHS" "Mechanical Plan - Exhaust diffusers, register and grilles" "Continuous" "35" "83" "1")

;; Get the outside points from the insertion point
(setq DiffPointList (DIFF_GET_POINTS InsPoint (DIFF_INSCRIBE_SQUARE DiffWidth)(DIFF_INSCRIBE_SQUARE DiffWidth)))
(setq ExhsPointList (list (nth 0 DiffPointList)(nth 1 DiffPointList)(nth 2 DiffPointList)))

;; Create list of 2D point from DiffPointList
(setq Stop (length DiffPointList))
(setq Counter 0)
(while (/= Counter Stop)
(setq TempPoint(reverse(cdr(reverse (nth Counter DiffPointList)))))
(setq DiffPoints (append TempPoint DiffPoints))
(setq Counter (1+ Counter))
)

;; Draw the outside of the diffuser from the 2D points
(setq vlaDiffuser (vlax-invoke space 'AddCircle InsPoint (/ DiffWidth 2)))

;; Offset the polyline for the layin edge
(setq DiffLayIn (car(vlax-invoke vlaDiffuser 'Offset -1)))

;; Draw line to show it is a supply diffuser
(foreach X ExhsPointList
(setq Ang (angle InsPoint X))
(setq StopPoint (polar X Ang (-(/ DiffInlet 2)(distance InsPoint X))))
(setq VlaLine (vlax-invoke space 'AddLine X StopPoint))
;; Set properties
(vlax-put VlaLine 'Layer "M-DIFF-EXHS")
)
;; Draw the center inlet
(setq VlaCircle (vlax-invoke space 'AddCircle InsPoint (/ DiffInlet 2)))

;; Set the properties on the newly created objects
(vlax-put vlaDiffuser 'Layer "M-DIFF-EXHS")
(vlax-put VlaCircle 'Layer "M-DIFF-EXHS")
(vlax-put DiffLayIn 'Layer "M-DIFF-EXHS")
(vlax-put DiffLayIn 'Color "8")

;; Silent exit
(princ)
)
)
(DIFF_RESET_ENV)
)
;;; ------------ CREATE MOUNTED SQUARE DIFFUSER
(defun DIFF_MOUNTED_SQUARE(InsPoint DiffWidth DiffLength DiffInlet /
DiffPointList DiffPoints ExhsPointList RtrnPointList Stop Counter TempPoint vlaDiffuser Ang StopPoint VlaLine VlaCircle)

(cond
((= (strcase DiffStyle)"SUPPLY")
;; Create the layer for the diffuser
(DIFF_CREATE_LAYER "M-DIFF-SUPP" "Mechanical Plan - Supply diffusers, register and grilles" "Continuous" "35" "133" "1")

;; Get the outside points from the insertion point
(setq DiffPointList(DIFF_GET_POINTS InsPoint DiffWidth DiffLength))

;; Create list of 2D point from DiffPointList
(setq Stop (length DiffPointList))
(setq Counter 0)
(while (/= Counter Stop)
(setq TempPoint(reverse(cdr(reverse (nth Counter DiffPointList)))))
(setq DiffPoints (append TempPoint DiffPoints))
(setq Counter (1+ Counter))
)

;; Draw the outside of the diffuser from the 2D points
(setq vlaDiffuser (vla-addLightweightPolyline Space
(vlax-safearray-fill
(vlax-make-safearray vlax-vbDouble (cons 0 (- (length DiffPoints) 1)))
DiffPoints))
)
;; Close the polyline
(vla-put-closed vlaDiffuser :vlax-true)

;; Draw line to show it is a supply diffuser
(foreach X DiffPointList
(setq Ang (angle InsPoint X))
(setq StopPoint (polar X Ang (-(/ DiffInlet 2)(distance InsPoint X))))
(setq VlaLine (vlax-invoke space 'AddLine X StopPoint))
;; Set properties
(vlax-put VlaLine 'Layer "M-DIFF-SUPP")
)
;; Draw the center inlet
(setq VlaCircle (vlax-invoke space 'AddCircle InsPoint (/ DiffInlet 2)))

;; Set the properties on the newly created objects
(vlax-put vlaDiffuser 'Layer "M-DIFF-SUPP")
(vlax-put VlaCircle 'Layer "M-DIFF-SUPP")

;; Silent exit
(princ)
)
((= (strcase DiffStyle)"RETURN")
;; Create the layer for the diffuser
(DIFF_CREATE_LAYER "M-DIFF-RETN" "Mechanical Plan - Return diffusers, register and grilles" "Continuous" "35" "23" "1")

;; Get the outside points from the insertion point
(setq DiffPointList(DIFF_GET_POINTS InsPoint DiffWidth DiffLength))
(setq RtrnPointList (list (nth 0 DiffPointList)(nth 2 DiffPointList)))

;; Create list of 2D point from DiffPointList
(setq Stop (length DiffPointList))
(setq Counter 0)
(while (/= Counter Stop)
(setq TempPoint(reverse(cdr(reverse (nth Counter DiffPointList)))))
(setq DiffPoints (append TempPoint DiffPoints))
(setq Counter (1+ Counter))
)

;; Draw the outside of the diffuser from the 2D points
(setq vlaDiffuser (vla-addLightweightPolyline Space
(vlax-safearray-fill
(vlax-make-safearray vlax-vbDouble (cons 0 (- (length DiffPoints) 1)))
DiffPoints))
)
;; Close the polyline
(vla-put-closed vlaDiffuser :vlax-true)

;; Draw line to show it is a supply diffuser
(foreach X RtrnPointList
(setq Ang (angle InsPoint X))
(setq StopPoint (polar X Ang (-(/ DiffInlet 2)(distance InsPoint X))))
(setq VlaLine (vlax-invoke space 'AddLine X StopPoint))
;; Set properties
(vlax-put VlaLine 'Layer "M-DIFF-RETN")
)
;; Draw the center inlet
(setq VlaCircle (vlax-invoke space 'AddCircle InsPoint (/ DiffInlet 2)))

;; Set the properties on the newly created objects
(vlax-put vlaDiffuser 'Layer "M-DIFF-RETN")
(vlax-put VlaCircle 'Layer "M-DIFF-RETN")

;; Silent exit
(princ)
)
((= (strcase DiffStyle)"EXHAUST")
;; Create the layer for the diffuser
(DIFF_CREATE_LAYER "M-DIFF-EXHS" "Mechanical Plan - Exhaust diffusers, register and grilles" "Continuous" "35" "83" "1")

;; Get the outside points from the insertion point
(setq DiffPointList(DIFF_GET_POINTS InsPoint DiffWidth DiffLength))
(setq ExhsPointList (list (nth 0 DiffPointList)(nth 1 DiffPointList)(nth 2 DiffPointList)))

;; Create list of 2D point from DiffPointList
(setq Stop (length DiffPointList))
(setq Counter 0)
(while (/= Counter Stop)
(setq TempPoint(reverse(cdr(reverse (nth Counter DiffPointList)))))
(setq DiffPoints (append TempPoint DiffPoints))
(setq Counter (1+ Counter))
)

;; Draw the outside of the diffuser from the 2D points
(setq vlaDiffuser (vla-addLightweightPolyline Space
(vlax-safearray-fill
(vlax-make-safearray vlax-vbDouble (cons 0 (- (length DiffPoints) 1)))
DiffPoints))
)
;; Close the polyline
(vla-put-closed vlaDiffuser :vlax-true)

;; Draw line to show it is a supply diffuser
(foreach X ExhsPointList
(setq Ang (angle InsPoint X))
(setq StopPoint (polar X Ang (-(/ DiffInlet 2)(distance InsPoint X))))
(setq VlaLine (vlax-invoke space 'AddLine X StopPoint))
;; Set properties
(vlax-put VlaLine 'Layer "M-DIFF-EXHS")
)
;; Draw the center inlet
(setq VlaCircle (vlax-invoke space 'AddCircle InsPoint (/ DiffInlet 2)))

;; Set the properties on the newly created objects
(vlax-put vlaDiffuser 'Layer "M-DIFF-EXHS")
(vlax-put VlaCircle 'Layer "M-DIFF-EXHS")

;; Silent exit
(princ)
)
)
(DIFF_RESET_ENV)
)
;;; ------------ CREATE MOUNTED ROUND DIFFUSER
(defun DIFF_MOUNTED_ROUND(InsPoint DiffWidth DiffInlet /
DiffPointList DiffPoints ExhsPointList RtrnPointList Stop Counter TempPoint vlaDiffuser Ang StopPoint VlaLine VlaCircle)

(cond
((= (strcase DiffStyle)"SUPPLY")
;; Create the layer for the diffuser
(DIFF_CREATE_LAYER "M-DIFF-SUPP" "Mechanical Plan - Supply diffusers, register and grilles" "Continuous" "35" "133" "1")

;; Get the outside points from the insertion point
(setq DiffPointList(DIFF_GET_POINTS InsPoint (DIFF_INSCRIBE_SQUARE DiffWidth)(DIFF_INSCRIBE_SQUARE DiffWidth)))

;; Create list of 2D point from DiffPointList
(setq Stop (length DiffPointList))
(setq Counter 0)
(while (/= Counter Stop)
(setq TempPoint(reverse(cdr(reverse (nth Counter DiffPointList)))))
(setq DiffPoints (append TempPoint DiffPoints))
(setq Counter (1+ Counter))
)

;; Draw the center inlet
(setq vlaDiffuser (vlax-invoke space 'AddCircle InsPoint (/ DiffWidth 2)))

;; Draw line to show it is a supply diffuser
(foreach X DiffPointList
(setq Ang (angle InsPoint X))
(setq StopPoint (polar X Ang (-(/ DiffInlet 2)(distance InsPoint X))))
(setq VlaLine (vlax-invoke space 'AddLine X StopPoint))
;; Set properties
(vlax-put VlaLine 'Layer "M-DIFF-SUPP")
)
;; Draw the center inlet
(setq VlaCircle (vlax-invoke space 'AddCircle InsPoint (/ DiffInlet 2)))

;; Set the properties on the newly created objects
(vlax-put vlaDiffuser 'Layer "M-DIFF-SUPP")
(vlax-put VlaCircle 'Layer "M-DIFF-SUPP")

;; Silent exit
(princ)
)
((= (strcase DiffStyle)"RETURN")
;; Create the layer for the diffuser
(DIFF_CREATE_LAYER "M-DIFF-RETN" "Mechanical Plan - Return diffusers, register and grilles" "Continuous" "35" "23" "1")

;; Get the outside points from the insertion point
(setq DiffPointList(DIFF_GET_POINTS InsPoint (DIFF_INSCRIBE_SQUARE DiffWidth)(DIFF_INSCRIBE_SQUARE DiffWidth)))
(setq RtrnPointList (list (nth 0 DiffPointList)(nth 2 DiffPointList)))

;; Create list of 2D point from DiffPointList
(setq Stop (length DiffPointList))
(setq Counter 0)
(while (/= Counter Stop)
(setq TempPoint(reverse(cdr(reverse (nth Counter DiffPointList)))))
(setq DiffPoints (append TempPoint DiffPoints))
(setq Counter (1+ Counter))
)

;; Draw the outside of the diffuser from the 2D points
(setq vlaDiffuser (vlax-invoke space 'AddCircle InsPoint (/ DiffWidth 2)))

;; Draw line to show it is a return diffuser
(foreach X RtrnPointList
(setq Ang (angle InsPoint X))
(setq StopPoint (polar X Ang (-(/ DiffInlet 2)(distance InsPoint X))))
(setq VlaLine (vlax-invoke space 'AddLine X StopPoint))
;; Set properties
(vlax-put VlaLine 'Layer "M-DIFF-RETN")
)
;; Draw the center inlet
(setq VlaCircle (vlax-invoke space 'AddCircle InsPoint (/ DiffInlet 2)))

;; Set the properties on the newly created objects
(vlax-put vlaDiffuser 'Layer "M-DIFF-RETN")
(vlax-put VlaCircle 'Layer "M-DIFF-RETN")

;; Silent exit
(princ)
)
((= (strcase DiffStyle)"EXHAUST")
;; Create the layer for the diffuser
(DIFF_CREATE_LAYER "M-DIFF-EXHS" "Mechanical Plan - Exhaust diffusers, register and grilles" "Continuous" "35" "83" "1")

;; Get the outside points from the insertion point
(setq DiffPointList (DIFF_GET_POINTS InsPoint (DIFF_INSCRIBE_SQUARE DiffWidth)(DIFF_INSCRIBE_SQUARE DiffWidth)))
(setq ExhsPointList (list (nth 0 DiffPointList)(nth 1 DiffPointList)(nth 2 DiffPointList)))

;; Create list of 2D point from DiffPointList
(setq Stop (length DiffPointList))
(setq Counter 0)
(while (/= Counter Stop)
(setq TempPoint(reverse(cdr(reverse (nth Counter DiffPointList)))))
(setq DiffPoints (append TempPoint DiffPoints))
(setq Counter (1+ Counter))
)

;; Draw the outside of the diffuser from the 2D points
(setq vlaDiffuser (vlax-invoke space 'AddCircle InsPoint (/ DiffWidth 2)))

;; Draw line to show it is a supply diffuser
(foreach X ExhsPointList
(setq Ang (angle InsPoint X))
(setq StopPoint (polar X Ang (-(/ DiffInlet 2)(distance InsPoint X))))
(setq VlaLine (vlax-invoke space 'AddLine X StopPoint))
;; Set properties
(vlax-put VlaLine 'Layer "M-DIFF-EXHS")
)
;; Draw the center inlet
(setq VlaCircle (vlax-invoke space 'AddCircle InsPoint (/ DiffInlet 2)))

;; Set the properties on the newly created objects
(vlax-put vlaDiffuser 'Layer "M-DIFF-EXHS")
(vlax-put VlaCircle 'Layer "M-DIFF-EXHS")

;; Silent exit
(princ)
)
)
(DIFF_RESET_ENV)
)
;;; ------------ GET CORNER POINTS FROM CENTER POINT
(defun DIFF_GET_POINTS (InsPoint DiffWidth DiffLength / DLength DWidth )

(setq DLength (* 0.5 DiffWidth))
(setq DWidth (* 0.5 DiffLength))
(setq InsPoint (trans InsPoint 1 0))
(setq DPoint1 (list (- (car InsPoint) DLength)(- (cadr InsPoint) DWidth)(caddr InsPoint)))
(setq DPoint2 (list (+ (car InsPoint) DLength)(+ (cadr InsPoint) DWidth)(caddr InsPoint)))
(setq DPoint3 (list (car DPoint2)(cadr DPoint1)(caddr InsPoint)))
(setq DPoint4 (list (car DPoint1)(cadr DPoint2)(caddr InsPoint)))
(list DPoint1 DPoint3 DPoint2 DPoint4)
)
;;; ------------ DEGREES TO RADIANS SUB ROUTINE
(defun DIFF_DTR (NumberOfDegrees)
(* pi (/ NumberOfDegrees 180.0))
)
;;; ------------ ROUND NUMBER
(defun DIFF_RND (Number Precision)
(setq Number(distof (rtos Number 4 Precision)4))
)
;;; ------------ INSCRIBE A SQUARE IN A CIRCLE
(defun DIFF_INSCRIBE_SQUARE (Diameter / A SinA SinB Sidelength)

(setq A Diameter)
(setq SinA (sin (DIFF_DTR 90)))
(setq SinB (sin (DIFF_DTR 45)))
(setq SideLength (DIFF_RND (/ (* A SinB) SinA)4))
Sidelength
)
;;; ------------ SET ENVIROMENT BEFORE LAUNCH
(defun DIFF_SET_ENV(/)

(setq OldClayer (getvar "CLAYER"))
(setq OldCmdEcho (getvar "CMDECHO"))

(setvar "CMDECHO" 0)

(vl-load-com)
(setq ActiveDoc (vla-get-activedocument (vlax-get-acad-object)))
(setq Space
(if (= (getvar "cvport") 1)
(vla-get-paperspace ActiveDoc)
(vla-get-modelspace ActiveDoc)
)
)
;; Run duct
(DIFF_RUN)
)
;;; ------------ RESET SYSEM VARIABLES
(defun DIFF_RESET_ENV (/)

(setvar "CMDECHO" OldCmdEcho)
(setvar "CLAYER" OldClayer)
(princ)
)
;;;
;;; Echos to the command line
(princ "\n CreateDiffuser v1.1 ©Timothy Spangler, \n January, 2007....loaded.")
(terpri)
(princ "C:DIFF")
(print)
;;; End echo

Advertisements