Community
오토캐드 AutoCAD - 한국어
프로그램에 관한 사용 방법, 기술, 정보 등을 검색하고, 질문을 통해 서로 도움을 주고 받을 수 있습니다.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

AutoLISP(3): Change Object Color

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
WeTanks
300 Views, 2 Replies

AutoLISP(3): Change Object Color

WeTanks_0-1718008321137.png

 

;;;------------------------------------------------------------;;;
;;; By WETANKS
;;; Version 1   -    6-10-2024
;;;------------------------------------------------------------;;;
(defun C:WE_To160 ()
  (setq ss (ssget))
  (if ss
    (progn
      (if (not (tblsearch "layer" "160"))
        (command "._layer" "make" "160" "color" "160" "" "")
      )
      (command "._change" ss "" "p" "la" "160" "")
      (command "._layer" "set" "0" "")
    )
  )
  (setq ss nil)
  (princ)
)
;;;------------------------------------------------------------;;;
;;; End of File
;;;------------------------------------------------------------;;;

 

 

 

We.Tanks



A couple of Fusion 360 improvement ideas that could your vote/support:
図面一括印刷

2 REPLIES 2
Message 2 of 3
exceeds
in reply to: WeTanks

한글로 설명(사족)을 달자면 

 

객체의 layer를 바꿔서 색상을 변경하는 루틴입니다.

단, 선택한 객체의 색상이 by layer로 설정되어있는 상태 & 160 이라는 레이어의 색상이 160으로 지정되어있을 때 유효합니다.

 

제목에 있는대로, 그냥 객체의 색상만 변경하려는 경우 

간단하게 선택세트에 직접 change를 실행하면 됩니다.

 

 

(command "change" (ssget) "" "p" "color" "160" "")

 

 

 

해당 루틴은 

tblsearch로 해당 도면에서 레이어 이름이 160인 레이어를 찾고,

그 레이어가 없는 경우, 색상을 160으로 설정한 160이라는 이름의 레이어를 생성하고, 

 

change로 해당 선택세트의 객체들을 160 레이어로 이동한 뒤에

현재 레이어를 0으로 되돌리는 것 까지 수행합니다.

(-layer > make로 레이어를 만들면 새로 만든 레이어가 현재 레이어로 설정되므로)

 

 

 

layer의 경우 entmake로 만드는 것이 다른 entity의 entmake보다 좀 더 귀찮으므로

예시대로 command로 만드는 것이 좀 더 편할 수 있습니다. 이 역시 2편과 같이 (setvar 'cmdecho 0 > 1)을 해주는게 좋겠죠..

 

entmake로 만든다면, 아래와 같이

 

 

 (entmake (list (cons 0 "LAYER")
                (cons 100 "AcDbSymbolTableRecord")
                (cons 100 "AcDbLayerTableRecord")
                (cons 2 레이어이름)
                (cons 70 동결잠금)
                (cons 62 색상)
                (cons 6 라인타입)
                (cons 290 플롯)
                (cons 370 선가중치)
             )
  )

 

 

한글로 지정된 곳에 해당하는 변수를 넣어주면 됩니다.

레이어 이름은 string 이면서 레이어 이름으로 할 수 있는 형식을 지켜야 하고,

동결 잠금은 비트 합산 (1bit - 동결, 3bit - 잠금) > 0 (동결x, 잠금x), 1 (동결o, 잠금x), 4 (동결x, 잠금o), 5 (동결o, 잠금o)

색상은 0 - by block, 256 - by layer, 1~255 aci color index 번호 + 양수면 layer on, 음수면 layer off

라인 타입은 스트링으로 라인 타입 이름을 넣어주면 되고 > "Continuous"와 같이

플롯은 1 과 0 boolean으로 yes no

선가중치는 mm단위에 100을 곱한 값을 넣습니다. -3이면 default 고, line weight 목록에 없는 값을 넣으면 기본값으로 적용됩니다. lwunits가 0으로 되어있어도 (imperial)로 되어있어도 mm환산 값을 넣어줘야 합니다. (ex 0.008" ≒ 0.02mm = 20)

 

여기에 투명도와 description을 추가하려면 조금 더 복잡해지는데

 

 

; layname - string, layer name
; layonoff - (1 = visible, 0 = invisible)
; layfrozen - (1 = frozen, 0 = thaw)
; laylock - (1 = lock, 0 = unlock)
; laycolor - integer, layer color (0 = by block, 256 = by layer, 1~255 aci color index number)
; laylinetype - string, layer linetype
; layplot - (1 = plot, 0 = non plot)
; laylineweight - (-3 = default, lineweight*100 (ex - 0.20mm = 20)), by metric, whatever you set by inches (lwunits = 0) (ex 0.008" = 20))
; laytransparency - integer, 0 to 90
; laydescription - string, layer description 

(defun EX:MakeLayer ( layname layonoff layfrozen laylock laycolor laylinetype layplot laylineweight laytransparency laydescription )
  (regapp "AcAecLayerStandard")
  (regapp "AcCmTransparency")
  ;LM:trans->dxf by Lee Mac ( https://www.theswamp.org/index.php?topic=52473.msg574001#msg574001 )
  (defun LM:trans->dxf ( x )
    (logior (fix (/ (* (- (expt 2 8) 1) (- 100 x)) 100)) (expt 2 25))
  )
  (entmake (list (cons 0 "LAYER") (cons 100 "AcDbSymbolTableRecord") (cons 100 "AcDbLayerTableRecord") 
                   (cons 2 layname) (cons 70 0) 
                   (cons 62 (* (- layonoff 1) laycolor))
                   (cons 6 laylinetype) 
                   (cons 70 (+ layfrozen (* 4 laylock)))
                   (cons 290 layplot)
                   (cons 370 laylineweight) 
                   (list -3 
                     (list "AcAecLayerStandard" (cons 1000 "") (cons 1000 laydescription))
                     (list "AcCmTransparency" (cons 1071 (LM:trans->dxf laytransparency))) 
                   )
              )
  )
)

 

 

와 같이 dxf 값으로 넣거나 (25비트에 1이 들어가야 하고, 투명도 %를 0~255 (8비트) 값으로 변환해야 합니다.)

 

투명도 값 변환 없이 쓰려면 setpropertyvalue를 사용해 넣을 수도 있습니다.

 

 

(defun EX:MakeLayer2 ( layname layonoff layfrozen laylock laycolor laylinetype layplot laylineweight laytransparency laydescription / nlay )
  (setq nlay 
    (entmakex (list (cons 0 "LAYER") (cons 100 "AcDbSymbolTableRecord") (cons 100 "AcDbLayerTableRecord") 
                   (cons 2 layname) (cons 70 0) 
                   (cons 62 (* (- layonoff 1) laycolor))
                   (cons 6 laylinetype) 
                   (cons 70 (+ layfrozen (* 4 laylock)))
                   (cons 290 layplot)
                   (cons 370 laylineweight) 
                )
    )
  )
  (setpropertyvalue nlay "Transparency" laytransparency)
  (setpropertyvalue nlay "Description" laydescription) ; or (vla-put-description (vlax-ename->vla-object nlay) laydescription)
  nlay
)

 

 

 

 

마지막으로 

 

(setq ss nil)

 

로 사용한 선택 세트 변수를 nil값으로 초기화해주는데,

 

그냥 지역변수로 바꿔줘도 되겠습니다.

 

(defun C:WE_To160 ( / ss)

 

Message 3 of 3
WeTanks
in reply to: exceeds

Very, very good.

Although the entmake function is efficient, it is difficult for beginners to understand. So I try not to use it.

I hope to create a good AutoLISP exchange place for everyone here.

We.Tanks



A couple of Fusion 360 improvement ideas that could your vote/support:
図面一括印刷

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums