- 积分
- 16166
- 明经币
- 个
- 注册时间
- 2011-11-21
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|

楼主 |
发表于 2012-3-7 12:24:36
|
显示全部楼层
;CAD和CASS模式自动识别 自动识别是否带高程 支持逗号分隔的TXT文件 展点 activex>entmake>command 此程序已经进行最优化
(vl-load-com)
(defun DSX-TypeLib-Excel ( / sysdrv tlb)
(setq sysdrv (getenv "systemdrive"))
(cond
( (setq tlb (findfile (strcat sysdrv "\\Program Files\\Microsoft Office\\Office\\Excel8.olb")))
tlb
)
( (setq tlb (findfile (strcat sysdrv "\\Program Files\\Microsoft Office\\Office\\Excel9.olb")))
tlb
)
( (setq tlb (findfile (strcat sysdrv "\\Program Files\\Microsoft Office\\Office\\Excel10.olb")))
tlb
)
( (setq tlb (findfile (strcat sysdrv "\\Program Files\\Microsoft Office\\Office\\Excel.exe")))
tlb
)
( (setq tlb (findfile (strcat sysdrv "\\Program Files\\Microsoft Office\\Office10\\Excel.exe")))
tlb
)
)
)
(defun DSX-Load-TypeLib-Excel ( / tlbfile tlbver out)
(cond
( (null msxl-xl24HourClock)
(if (setq tlbfile (DSX-TypeLib-Excel))
(progn
(setq tlbver (substr (vl-filename-base tlbfile) 6))
(cond
( (= tlbver "9")
(princ "\n初始化 Microsoft Excel 2000...") )
( (= tlbver "8")
(princ "\n初始化 Microsoft Excel 97...") )
( (= (vl-filename-base tlbfile) "Excel.exe")
(princ "\n初始化 Microsoft Excel XP...")
)
)
(vlax-import-type-library
:tlb-filename tlbfile
:methods-prefix "msxl-"
:properties-prefix "msxl-"
:constants-prefix "msxl-"
)
(if msxl-xl24HourClock (setq out T))
)
)
)
( T (setq out T) )
)
out
)
(defun DSX-Open-Excel-Exist (xfile dmode / appsession)
(princ "\n打开 Excel 电子表格文件...")
(cond
( (setq fn (findfile xfile))
(cond
( (setq appsession (vlax-get-or-create-object "Excel.Application"))
(vlax-invoke-method
(vlax-get-property appsession 'WorkBooks)
'Open fn
)
(if (= (strcase dmode) "SHOW")
(vla-put-visible appsession 1)
(vla-put-visible appsession 0)
)
)
)
)
( T (alert (strcat "\n不能找到指定的文件: " xfile)) )
)
appsession
)
;在活动的工作表中的单个单元格中获取数据
;;; 获取行<relrow> 和列 <relcol>范围内的单元格对象
(defun DSX-Excel-Get-Cell (rng relrow relcol)
(vlax-variant-value
(msxl-get-item (msxl-get-cells rng)
(vlax-make-variant relrow)
(vlax-make-variant relcol)
)
)
)
;返回单元格(row, col)内容的值
(defun DSX-Excel-Get-CellValue (row col)
(vlax-variant-value
(msxl-get-value
(DSX-Excel-Get-Cell
(msxl-get-ActiveSheet xlapp)
row col
)
)
)
)
(setq *ModelSpace* (vla-get-ModelSpace (vla-get-ActiveDocument
(vlax-get-Acad-Object))))
(defun list->variantArray (ptsList / arraySpace sArray)
; 给以双精度实数表示的二维点数组分配空间
(setq arraySpace (vlax-make-safearray
vlax-vbdouble ; 元素类型
(cons 0
(- (length ptsList) 1)
) ; 数组维数
)
)
(setq sArray (vlax-safearray-fill arraySpace ptsList))
; 返回数组变体
(vlax-make-variant sArray)
)
(defun Create_activeX_Point()
(setq AcadObject(vlax-get-acad-object)
AcadDocument(vla-get-ActiveDocument AcadObject)
)
(setq LEN (length NM))
(if (= (nth 2 dialogResults) 1)
(progn
(setq count 0)
(setq LayerSel ( vla-get-Layers AcadDocument))
(setq NewLayer (vla-add LayerSel "zdh"))
(vla-put-ActiveLayer AcadDocument NewLayer)
(vla-put-Color NewLayer acRed)
;(command "clayer" "zdh")
(while (< count LEN)
(if (= CASSMODE 1)
(progn
(if (/= pos3 nil)
(setq VLADataPts (list->variantArray (list ( + ( * (nth 0 (nth count PT) ) 2) 100) ( + ( * (nth 1 (nth count PT) ) 2) 100) (distof (nth count GCVA)) ) ) )
(setq VLADataPts (list->variantArray (list ( + ( * (nth 0 (nth count PT) ) 2) 100) ( + ( * (nth 1 (nth count PT) ) 2) 100) 0 ) ) )
);end if
);end progn
(progn
(if (/= pos3 nil)
(setq VLADataPts (list->variantArray (list (nth 0 (nth count PT) ) (nth 1 (nth count PT) ) (distof (nth count GCVA)) ) ) )
(setq VLADataPts (list->variantArray (list (nth 0 (nth count PT) ) (nth 1 (nth count PT) ) 0 ) ) )
)
)
);end if
(setq ACTIVEXPOINT (vla-AddPoint *ModelSpace* VLADataPts )) ;控制台下shirft+ctrl+space 显示所有vla-add函数
(setq count (+ count 1))
)
)
);end if
(if (= (nth 1 dialogResults) 1)
(progn
(setq count 0)
;(command "clayer" "dh")
(setq NewLayer (vla-add LayerSel "dh"))
(vla-put-ActiveLayer AcadDocument NewLayer)
(vla-put-Color NewLayer acRed)
(while (< count LEN)
(if (= CASSMODE 1)
(setq VLADataT1 (list->variantArray(list (- ( + ( * (nth 0 (nth count PT) ) 2 ) 100) 4) ( + ( * (nth 1 (nth count PT) ) 2 ) 100 ) 0) ) )
(setq VLADataT1 (list->variantArray(list (- (nth 0 (nth count PT) ) 4) (nth 1 (nth count PT) ) 0) ) )
);end if
(setq ACTIVEXTEXT1 (vla-AddText *ModelSpace* (nth count NM) VLADataT1 2 ))
(setq count (+ count 1))
)
)
);end if
(if (and (= (nth 3 dialogResults) 1) (/= pos3 nil))
(progn
(setq count 0)
;(command "clayer" "gcd")
(setq NewLayer (vla-add LayerSel "gcd"))
(vla-put-ActiveLayer AcadDocument NewLayer)
(vla-put-Color NewLayer acRed)
(while (< count LEN)
(if ( = CASSMODE 1 )
(progn
(setq VLADataT2 (list->variantArray(list (+ ( * (nth 0 (nth count PT) ) 2) 100 ) ( + ( * (nth 1 (nth count PT) ) 2) 100 ) 0 ) ) )
)
(progn
(setq VLADataT2 (list->variantArray(list (nth 0 (nth count PT) ) (nth 1 (nth count PT) ) 0 ) ) )
)
);end if
(setq ACTIVEXTEXT2 (vla-AddText *ModelSpace* (nth count GCVA) VLADataT2 2 ))
(setq count (+ count 1))
)
)
);end if
);_ end of defun
(defun Create_entmake_Point()
(setq LEN (length NM))
(if(= (nth 2 dialogResults) 1);如果展位
(progn
(setq count 0)
(setq a1 (cons 0 "point"))
(setq a3 (cons 8 "zdh"))
(setq color (cons 62 1))
(while (< count LEN)
(if (= CASSMODE 1)
(progn
(if (/= pos3 nil)
(setq a2 (cons 10 (list (+ ( * (nth 0 (nth count PT)) 2) 100 ) ( +( * (nth 1 (nth count PT) ) 2 ) 100) (distof (nth count GCVA))) ))
(setq a2 (cons 10 (list (+ ( * (nth 0 (nth count PT)) 2) 100 ) ( +( * (nth 1 (nth count PT) ) 2 ) 100) 0 ) ))
);end if
);end progn
(progn
(if (/= pos3 nil)
(setq a2 (cons 10 (list (nth 0 (nth count PT)) (nth 1 (nth count PT) ) (distof (nth count GCVA))) ))
(setq a2 (cons 10 (list (nth 0 (nth count PT)) (nth 1 (nth count PT) ) 0 ) ))
);END IF
);end progn
);END IF
(entmake (list a1 a2 a3 color))
(setq count (+ count 1))
);END WHILE
)
);end if
(if (= (nth 1 dialogResults) 1)
(progn
(setq count 0)
(setq b1 (cons 0 "text"))
(setq b4 (cons 40 2.0))
(setq b5 (cons 8 "dh"))
(setq b6 (cons 50 0))
(while (< count LEN)
(setq b2 (cons 1 (nth count NM)))
(if (= CASSMODE 1)
(setq b3 (cons 10 (list (- (+ ( * (nth 0 (nth count PT)) 2 ) 100 ) 12) (+ ( * (nth 1 (nth count PT) ) 2) 100) )))
(setq b3 (cons 10 (list (- (nth 0 (nth count PT)) 12) (nth 1 (nth count PT) ) )))
)
(setq b3 (append b3 (list 0)))
(entmake (list b1 b2 b3 b4 b5 b6 color ))
(setq count (+ count 1))
)
)
);end if
(if (and (= (nth 3 dialogResults) 1) (/= pos3 nil))
(progn
(setq count 0)
(setq c1 (cons 8 "gcd"))
(while (< count LEN)
(setq c3 (cons 1 (nth count GCVA)))
(if ( = CASSMODE 1)
(setq c2 (cons 10 (list ( + ( * (nth 0 (nth count PT)) 2) 100 ) ( + ( * (nth 1 (nth count PT) ) 2 ) 100) )))
(setq c2 (cons 10 (list (nth 0 (nth count PT)) (nth 1 (nth count PT) ) )))
)
(setq c2 (append c2(list 0)))
(entmake (list b1 c3 b4 b6 c1 c2 color ))
(setq count (+ count 1))
)
)
);end if
) ;_ end of defun
(defun Create_command_Point ()
(setq LEN (length NM))
(if (= (nth 2 dialogResults) 1)
(progn
(setq count 0)
(command ".clayer" "zdh")
(while(< count LEN)
( if (= CASSMODE 1)
(PROGN
( if (/= pos3 nil)
(command "point" (list (+ ( * (nth 0 (nth count PT) ) 2) 100) (+ ( * (nth 1 (nth count PT) )2)100) (distof (nth count GCVA))) )
(command "point" (list ( + ( * (nth 0 (nth count PT) ) 2 )100) ( + ( * ( nth 1 (nth count PT) )2)100) 0) )
);end if
);end progn
(progn
(if (/= pos3 nil)
(command "point" (list (nth 0 (nth count PT) ) (nth 1 (nth count PT) ) (distof (nth count GCVA))) )
(command "point" (list (nth 0 (nth count PT) ) (nth 1 (nth count PT) ) 0) )
);end if
);end progn
);END IF
(setq count (+ count 1) )
)
)
);end if
(if (= (nth 1 dialogResults) 1)
(progn
(setq count 0)
(command ".layer" "n" "dh" "c" 1 "dh" "")
(command ".clayer" "dh")
(while (< count LEN )
( if(= CASSMODE 1)
(command "text" (list (- (+(*(nth 0 (nth count PT) )2)100) 12) (+(*(nth 1 (nth count PT) )2)100) ) 2 0 (nth count NM) )
(command "text" (list (- (nth 0 (nth count PT) ) 12) (nth 1 (nth count PT) ) ) 2 0 (nth count NM) )
);END IF
(setq count (+ count 1) )
)
)
);end if
(if(and (= (nth 3 dialogResults) 1) (/= pos3 nil))
(progn
(setq count 0)
(command ".clayer" "gcd")
(while (< count LEN )
(if (= CASSMODE 1)
(command "text" (list (+(*(nth 0 (nth count PT) )2)100) (+(*(nth 1 (nth count PT) )2)100) ) 2 0 (nth count GCVA) )
(command "text" (list (nth 0 (nth count PT) ) (nth 1 (nth count PT) ) ) 2 0 (nth count GCVA) )
);END IF
(setq count (+ count 1) )
)
)
);end if
) ;_ end of defun
(defun dialoginput()
(setq dialogLoaded T)
(setq dialogShow T)
(setq zdfs1 0)
(setq zdfs2 0)
(setq zdfs3 0)
(if (= -1 (setq dcl_id (load_dialog "zzd1.dcl")))
(progn
;; There's a problem - display a message and set the
;; dialogLoaded flag to nil
(princ "\nERROR: Cannot load gpdialog.dcl")
(setq dialogLoaded nil)
) ;_ end of progn
)
(if (and dialogLoaded
(not (new_dialog "zddialog" dcl_id))
) ;_ end of and
(progn
;; There's a problem...
(princ "\nERROR: Cannot show dialog gp_mainDialog")
(setq dialogShow nil)
) ;_ end of progn
)
(if (and dialogLoaded dialogShow)
(progn
(action_tile
"gp_file"
(strcat "(progn (setq filedir (getfiled \"打开文件\" \" \" \"*\" 0))"
"(SET_TILE \"gp_filename\" filedir))"
)
)
(action_tile
"gp_dh"
"(setq zdfs1 1)"
)
(action_tile
"gp_zb"
"(setq zdfs2 1)"
)
(action_tile
"gp_gc"
"(setq zdfs3 1)"
)
;; Assign the actions (the functions to be invoked) to the dialog buttons
(action_tile
"gp_actx"
"(setq objectCreateMethod \"ActiveX\")"
) ;_ end of action_tile
(action_tile
"gp_emake"
"(setq objectCreateMethod \"Entmake\")"
) ;_ end of action_tile
(action_tile
"gp_cmd"
"(setq objectCreateMethod \"Command\")"
) ;_ end of action_tile
(action_tile "cancel" "(done_dialog) (setq UserClick nil)")
(action_tile
"accept"
"(done_dialog) (setq UserClick T))"
) ;_ end of action_tile
;; Now that everything is set and ready to go, invoke the dialog.
;; Once it is on-screen, it controls the program flow, until the
;; user hits OK or cancel
(start_dialog)
;; OK or cancel has been hit, you're out of the dialog. Unload it
(unload_dialog dcl_id)
;; Check for the value of the variable userClick. This determines if
;; the user selected OK or cancel, and is represented by a value
;; of T or nil
(if UserClick ; User clicked Ok
;; Build the resulting data
(progn
(setq Result (list ))
(setq Result (cons objectCreateMethod Result ) );_ end of list
(setq Result (append Result (list zdfs1 zdfs2 zdfs3 ) ))
;_ end of setq
) ;_ end of progn
) ;_ end of if
) ;_ end of progn
Result
)
);dialog
(defun c:ZD()
(setq dmode "HIDE")
(setq echo (getvar "cmdecho"))
(setvar "cmdecho" 0)
(setq CASS (getvar "useri5"))
(setq CASSMODE 0)
(if (= CASS 666)
(setq CASSMODE 1)
)
;(setq filedir (getfiled "打开文件" "" "" 2))
;(setq fp (open filedir "r"))
(setq dialogResults (dialoginput));s所有返回值都在dialogrResults序列中
(setq oldlayer (getvar "clayer"))
(setq fileexten (vl-filename-extension filedir) )
(setq PT (list ))
(setq NM (list ))
(setq GCVA(list ))
(setq ROWALL 2)
(if (= fileexten ".xls")
( progn
;(setq dmode "SHOW")
(DSX-Load-TypeLib-Excel)
(setq xlapp (DSX-Open-Excel-Exist filedir dmode))
;怎么获得行和列的总数 ?
(while (/= ( DSX-Excel-Get-CellValue ROWALL 1 ) nil)
(setq CellDh ( DSX-Excel-Get-CellValue 1 1 )) ;读取第一行第一列
(setq CellX ( DSX-Excel-Get-CellValue 1 2 ))
(setq CellY ( DSX-Excel-Get-CellValue 1 3 ))
(setq CellGc ( DSX-Excel-Get-CellValue 1 4 ))
(setq ROWALL (+ ROWALL 1))
);end while 一直读到最后一行
(setq NM (append (list CellDh) NM))
(setq PT (cons (list CellX CellY ) PT))
(setq CellGcv (rtos CellGc 2 3))
(setq GCVA (append (list CellGcv) GCVA))
(setq pos3 1 )
)
(progn
(setq fp (open filedir "r"))
(while (/= (setq data( read-line fp) ) nil )
(setq strlength (strlen data));base 1
(setq pos1 (vl-string-search "," data ) );读出第一个,的位置
(setq name (substr data 1 pos1) );由于vl-string-search base 是从 0开始 而 substr base是从1 开始
(setq pos2 (vl-string-search "," data (+ pos1 1)));查找下一个,的位置
(setq pointxstr (substr data (+ pos1 2) (- pos2 pos1 1 ) ));提取x坐标的字符串
(setq pointx (atof pointxstr) )
(if ( = (setq pos3 (vl-string-search "," data (+ pos2 1) )) nil);如果不带高程
(progn
(setq pointystr (substr data (+ pos2 2) (- strlength pos2 1) ))
(setq pointy (atof pointystr))
)
(progn
(setq pointystr (substr data (+ pos2 2) (- pos3 pos2 1) ) )
(setq pointy (atof pointystr))
(setq gcv (substr data (+ pos3 2) (- strlength pos3 2) ))
(setq gcv (distof gcv ))
(setq gcv (rtos gcv 2 3))
)
);end if
;(command "clayer" "zdh")
;(command "layer" "set" "zdh" " ")
;(command "point" (list pointy pointx 0) )
;(command "clayer" "dh")
;(command "layer" "set" "dh" " ")
;(command "text" (list (- pointy 12) pointx ) 2 0 name )
;(command "clayer" "gcd")
;(command "layer" "set" "gcd" " ")
;(command "text" (list pointy pointx ) 2 0 gcv )
;cond
(setq PT (cons (list pointy pointx ) PT))
(setq NM (append (list name) NM))
( if (/= pos3 nil)
(setq GCVA (append (list gcv) GCVA))
)
)
);end progn
);end if
(cond
((equal (strcase (car dialogResults)) "ACTIVEX")
(Create_activeX_Point)
)
((equal (strcase (car dialogResults)) "ENTMAKE")
(Create_entmake_Point)
)
((equal (strcase (car dialogResults)) "COMMAND")
(Create_command_Point)
)
(T nil )
);end cond
(command "clayer" oldlayer)
(close fp)
(setvar "cmdecho" echo)
) |
|