;;; ------------------------------------------------------------------------
;;; CreateCap.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:CAP (/ TrunkLine CapInsul)(CAP_START))
;;; ------------ MAIN FUNCTION
(defun CAP_START (/ *error* ActiveDoc Space OldClayer OldCmdEcho OldOsmode LineStart
LineEnd LineAngle TrunkSize CapStart SidePoint CapLayer EntList )

;;; 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)
)
(CAP_RESET_ENV)
(princ)
)
;;; End Error Handler ---------------------------------------------------
(CAP_SET_ENV)
)
;;; ------------ SET ENVIROMENT BEFORE LAUNCH
(defun CAP_SET_ENV(/)

;; Set system variables
(setq OldClayer (getvar "CLAYER"))
(setq OldCmdEcho (getvar "CMDECHO"))
(setq OldOsmode (getvar "OSMODE"))

(setvar "CMDECHO" 0)

;; Set undo marker
(command "_undo" "begin")

;; Load vlisp com
(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)
)
)

;; Setup layer for insulation
(CAP_CREATE_LAYER "M-HVAC-INSL" "Mechanical Plan - Ductwork Insulation" "HIDDEN2" "15" "201" "1")

;; Run cap
(CAP_RUN)
)
;;; ------------ RUN TEE SUB ROUTINE - GET VARIABLES
(defun CAP_RUN (/ TrunkLine EntList TeeLayer)

;; Get properties from current trunk line
(while (null (setq TrunkLine (car (nentsel "\n Select duct to add cap to: "))))
(princ "\n Duct not selected")
)
(setq EntList (entget TrunkLine))
(setq CapLayer (cdr (assoc 8 EntList)))

;; Set cap layer
(setvar "CLAYER" CapLayer)

;; Set cap properties
(if (equal (cdr (assoc 0 EntList)) "LINE")
(progn
(setq LineStart (cdr (assoc 10 EntList)))
(setq LineEnd (cdr (assoc 11 EntList)))
(setq LineAngle (angle LineStart LineEnd))
(setq TrunkSize (distance LineStart LineEnd))
(setq CapStart (polar LineStart LineAngle (/ (distance LineStart LineEnd) 2)))
)
(progn
(princ "\n Trunk must be a line. ")
(CAP_RUN)
)
)

;; Get cap side
(setq SidePoint (getpoint CapStart "\n Define end to cap: "))
(setq TrunkDirection (CAP_GET_PERP LineStart LineEnd SidePoint))

;; Get cap insulation
(initget 1 "None Inside Outside Both")
(setq CapInsul (getkword "\n Insulation options: (None/Inside/Oustside/Both)"))

(CAP_CREATE CapInsul)
)
;;; ------------ CREATE SYMETRICAL TEE SUB ROUTINE
(defun CAP_CREATE (CapInsul / CapPoint01 CapPoint02 CapPoint03 CapPoint04 CapPoint05
CapInsulPoint01 CapInsulPoint02 CapInsulPoint03 CapInsulPoint04 CapInsulPoint05 CapInsulPoint06)

;; Create cap outside points
(setq CapPoint01 (polar CapStart TrunkDirection 1.0))
(setq CapPoint02 (polar CapPoint01 (CAP_ADD_ANGLE TrunkDirection 90.0) (+ 1.0 (/ TrunkSize 2))))
(setq CapPoint03 (polar CapPoint01 (CAP_SUBTRACT_ANGLE TrunkDirection 90.0) (+ 1.0 (/ TrunkSize 2))))
(setq CapPoint04 (polar CapPoint02 (CAP_ADD_ANGLE TrunkDirection 180.0) 2.0))
(setq CapPoint05 (polar CapPoint03 (CAP_ADD_ANGLE TrunkDirection 180.0) 2.0))

;; Create cap insulation points
(setq CapInsulPoint01 (polar LineStart (CAP_ADD_ANGLE TrunkDirection 180.0) 1.0))
(setq CapInsulPoint02 (polar LineEnd (CAP_ADD_ANGLE TrunkDirection 180.0) 1.0))

(setq CapInsulPoint03 (polar CapPoint04 (CAP_ADD_ANGLE TrunkDirection 90.0) 1.0))
(setq CapInsulPoint04 (polar CapPoint05 (CAP_SUBTRACT_ANGLE TrunkDirection 90.0) 1.0))
(setq CapInsulPoint05 (polar CapInsulPoint03 TrunkDirection 3.0))
(setq CapInsulPoint06 (polar CapInsulPoint04 TrunkDirection 3.0))

;; Create cap
(vlax-invoke space 'addline CapPoint04 CapPoint02)
(vlax-invoke space 'addline CapPoint02 CapPoint03)
(vlax-invoke space 'addline CapPoint05 CapPoint03)

;; Set insulation layer
(setvar "CLAYER" "M-HVAC-INSL")

;; Create insulation
(cond
((= CapInsul "Inside")
(vlax-invoke space 'addline CapInsulPoint01 CapInsulPoint02)
)
((= CapInsul "Outside")
(vlax-invoke space 'addline CapInsulPoint03 CapInsulPoint05)
(vlax-invoke space 'addline CapInsulPoint05 CapInsulPoint06)
(vlax-invoke space 'addline CapInsulPoint04 CapInsulPoint06)
)
((= CapInsul "Both")
(vlax-invoke space 'addline CapInsulPoint01 CapInsulPoint02)
(vlax-invoke space 'addline CapInsulPoint03 CapInsulPoint05)
(vlax-invoke space 'addline CapInsulPoint05 CapInsulPoint06)
(vlax-invoke space 'addline CapInsulPoint04 CapInsulPoint06)
)
)

;; Reset environment
(CAP_RESET_ENV)
)
;;; ------------ LAYER CREATION ROUINE
(defun CAP_CREATE_LAYER (Layer Descpition Linetype Thickness Color Plot / Linetype TmpList VLA-Obj)

;; Check to see if linetype exsists
(if (= (tblsearch "ltype" Linetype) nil)
(if (CAP_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 CAP_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(CAP_STRING_TO_LIST CurrentLine ","))2))
(if (= (strcase Linetype) LinetypeName)
(setq Result T)
)
)
)
)
(close OpenFile)
Result
)
;;; ------------ STRING TO LIST SUB ROUTINE
(defun CAP_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))
)
;;; ------------ ADD / SUBTRACT ANGLE SUB ROUTINES
(defun CAP_ADD_ANGLE (Radians AddAngle / )

(CAP_DTR(+ (CAP_RTD Radians) AddAngle))
)
(defun CAP_SUBTRACT_ANGLE (Radians AddAngle / )

(CAP_DTR(- (CAP_RTD Radians) AddAngle))
)

;;; ------------ GET PERPENDICULAR POINT
(defun CAP_GET_PERP (StartPoint EndPoint Point / EntList LineStart LineEnd LineAngle NewAngle PerpAngle)

(setq PerpStart (trans StartPoint 0 1))
(setq PerpEnd (trans EndPoint 0 1))
(setq PerpAngle (angle PerpStart PerpEnd))

(if (minusp (sin (- (angle PerpStart Point) PerpAngle))) ;determine direction
(setq NewAngle (- PerpAngle (/ pi 2))) ;if "below" -90 deg
(setq NewAngle (+ PerpAngle (/ pi 2))) ;or "above" +90 deg
)
NewAngle
)
;; ------------ DEGREES / RADIANS SUB ROUTINES
(defun CAP_DTR (NumberOfDegrees)
(* pi (/ NumberOfDegrees 180.0))
)
(defun CAP_RTD (NumberOfRadians)
(* 180.0 (/ NumberOfRadians pi))
)
;;; ------------ RESET SYSEM VARIABLES
(defun CAP_RESET_ENV (/)

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

Advertisements