war32 发表于 2018-1-18 15:35:26

通用打印函数,可指定参数,可打印pdf

;打印函数,lujing可为nil,orpdf为T或nil,为T是打印pdf,为nil是打印到设备
(defun gm-dayin(dayinji tufu p1 p2 xiankuan fenshu lujing orpdf yulan / acaddoc fangxiang jixu medianame mspace p3 plot pointtemp1 pointtemp2 st x x1 x2 y y1 y2)
;设定系统变量,确保cad在前台进行打印,这样后一次打印会在前一次打印完成后才开始,避免错误
(setvar "backgroundplot" 0)
;由p1和p2坐标判断是横向还是纵向
(if (> (cadr p1) (cadr p2)) (setq p3 p1
            p1 (list (car p1) (cadr p2) 0)
            p2 (list (car p2) (cadr p3) 0)))
(setq x1 (car p1) y1 (cadr p1) x2 (car p2) y2 (cadr p2))
(setq x (abs (- x1 x2)) y (abs (- y1 y2)) )
(if (> y x) (setq st "p") (setq st "l"))
;当路径不为nil时,给路径加上后缀名
(if (/= lujing nil)(setq lujing (strcat lujing ".pdf")))
(setq acaddoc (vla-get-activedocument(vlax-get-acad-object))
    mspace(vla-get-activelayout acaddoc)
    plot (vla-get-plot acaddoc)
)
(setq p1 (trans p1 02)
    p2 (trans p2 0 2)
)
;处理p1和p2
(setq p1 (reverse (cdr (reverse p1))))
(setq p2 (reverse (cdr (reverse p2))))
(setq pointTemp1 (vlax-make-safearray vlax-vbDouble '(0 . 1)))
(vlax-safearray-fill pointtemp1 p1)
(setq pointTemp2 (vlax-make-safearray vlax-vbDouble '(0 . 1)))
(vlax-safearray-fill pointtemp2 p2)

;设定打印机
(vla-put-configname mspace dayinji)
;设定打印样式表
(vla-put-stylesheet mspace "monochrome.ctb" )
(vla-refreshplotdeviceinfo mspace)
;设定打印区域
(vla-setwindowtoplot mspace pointtemp1 pointtemp2)
;设定打印形式为窗口打印,必须在设定打印区域之后
(vla-put-plottype mspace acWindow)
;设定布满图纸打印
(vla-put-standardscale mspace acscaletofit)
;设定是否居中打印
(vla-put-centerplot mspace :vlax-true)

;不居中打印时的设置
;(setq p3 (vlax-make-safearray vlax-vbDouble '(0 . 1)))
;(vlax-safearray-put-element p3 0 0)
;(vlax-safearray-put-element p3 1 0)
;(vla-put-plotorigin mspace p3)
;将图纸大小转换成标准介质名称返回
(setq medianame (gm-putlocalemedianame mspace tufu))
(vla-put-canonicalmedianame mspace medianame)
(vla-getpapersize mspace 'chang 'kuan)
(if (> chang kuan)(setq fangxiang "heng")(setq fangxiang "shu"))
;设定横向或者纵向,l横向,p纵向
(cond
    ((and (eq "heng" fangxiang) (= st "l")) (vla-put-plotrotation mspace ac0degrees))
    ((and (eq "shu" fangxiang) (= st "l")) (vla-put-plotrotation mspace ac90degrees))
    ((and (eq "heng" fangxiang) (= st "p")) (vla-put-plotrotation mspace ac90degrees))
    ((and (eq "shu" fangxiang) (= st "p")) (vla-put-plotrotation mspace ac0degrees))
    (T exit)
)

;设定是否打印对象线宽
(if (= (strcase xiankuan) "Y")
    (vla-put-plotwithlineweights mspace :vlax-true)
    (vla-put-plotwithlineweights mspace :vlax-false))

;打印预览
(if (= yulan "y")(progn
         (vla-displayplotpreview plot acfullpreview)
         
         (setq jixu (getstring "/n 打印本张(Y)不打印本张(N):(Y)"))
         (if (= jixu "") (setq jixu "y")))
    (setq jixu "y")
)


(setq jixu (strcase jixu))
;打印份数
(vla-put-numberofcopies plot fenshu)
(cond
    ((and (= jixu "Y") (= orpdf T)) (vla-plottofile plot lujing))
    ((and (= jixu "Y") (= orpdf nil)) (vla-plottodevice plot dayinji))
    (T nil)
)
;恢复系统变量
(setvar "backgroundplot" 0)
)
;;;返回图纸尺寸本地名称所对应的标准名称
(defun gm-putlocalemedianame(mspace bendiming / i localmedianame medianame medianames)
(setq medianames (vla-getcanonicalmedianames mspace)
    i 0
    medianames (vlax-safearray->list (vlax-variant-value medianames))
)

(while (< i (length medianames))
    (setq localmedianame (vla-getlocalemedianame mspace (nth i medianames)))
    (if (eq localmedianame bendiming) (setq medianame (nth i medianames)
                i (1+ (length medianames)))
      (setq i (1+ i))
    )
   
)
medianame
)

war32 发表于 2018-1-23 09:08:23

w245272914 发表于 2018-1-22 10:04
大神~有些看不懂啊

(defun c:tt()
       
        (gm-dayin "dwg to pdf.pc3" "ISO A3 (420.00 x 297.00 毫米)" (getpoint "\n 左上角:")(getpoint "\n 右下角:") "y" 1 "d:\\1.pdf" T "y" )
)
可以用这个试一下

w245272914 发表于 2018-1-23 18:02:43

war32 发表于 2018-1-23 09:08
(defun c:tt()
       
        (gm-dayin "dwg to pdf.pc3" "ISO A3 (420.00 x 297.00 毫米)" (getpoint "\n 左上角 ...

哥,你看~
指令: ttt

左上角:
右下角:; 錯誤: no function definition: GM-DAYIN

war32 发表于 2018-1-23 08:50:53

ssyfeng 发表于 2018-1-20 17:26
打印成文件的时候少了个文件名

看了下,没问题啊

孤独人 发表于 2018-1-19 14:02:17

谢谢分享:D:D

lijiayin110 发表于 2018-1-20 09:45:42

好像用VLA的函数比直接用command慢

434939575 发表于 2018-1-20 10:10:10

支持源码,我也是直接COMMAND的。VLA有的还不会。

ssyfeng 发表于 2018-1-20 17:26:18

打印成文件的时候少了个文件名

yshf 发表于 2018-1-22 08:14:00

谢谢分享

w245272914 发表于 2018-1-22 10:04:49

大神~有些看不懂啊
页: [1] 2
查看完整版本: 通用打印函数,可指定参数,可打印pdf