(defun C:draw_layout_and_viewport_be_format_block
       ( / flag block block_name name num list_layout
	new_layout name_layout viewport
	viewport_center_in_model block_name_list
	width height block_name block Scale Target_Point
	Rotation name_layout flag name_layout
	name_layout_list num num_list name name_list
	flag_nastr flag_back_model flag_shtamp symbol
	)
  (geo_tools-help "draw_layout_and_viewport_be_format_block")
  (load_global_variable)
;;;  (setq name_layout(file_dat_r_g_w "r" "name_layout" nil))
  (setq name_layout (PL:GetD "last_name_layout"))
  (if (eq name_layout nil) (setq name_layout "Лист1"))
  (setq name (car(extract_name_num name_layout nil)))
  (setq num (cadr(extract_name_num name_layout nil)))
  (setq name_layout nil)
  (while (not (eq 'STR (type name_layout)))
;;;    (setq flag_back_model(file_dat_r_g_w "r" "flag_back_model" nil))
    (setq flag_back_model (PL:GetD "flag_back_model"))
    (if (eq flag_back_model nil) (setq flag_back_model "Да"))
;;;    (setq flag_shtamp(file_dat_r_g_w "r" "shtamp_type" nil))
    (setq flag_shtamp (PL:GetD "last_shtamp_type"))
    (if (eq flag_shtamp nil) (setq flag_shtamp "Авто"))
  (if (eq patch nil) (setq patch patch_blank))
    (princ (strcat "\nТекущие настройки: Вернуться в модель: "flag_back_model"; Тип вставляемого штампа: "flag_shtamp))
    (initget "Настройки")(setq name_layout(getstring T (strcat  "\nНазвание листа [Настройки] <" name (itoa num)">: ")))
    (cond
      ((= name_layout "Н")
       (setq name_layout nil)
       (setq flag_nastr T)
       (while (not (eq flag_nastr nil))
	 (initget "Вернуться Тип")(setq flag_nastr (getkword (strcat"\n[Вернуться в модель/Тип штампа]<продолжить>:")))
	 (cond
	   ((eq flag_nastr "Вернуться")
	    (initget "Да Нет")(setq temp (getkword (strcat"\nВернуться в модель[Да/Нет]<"flag_back_model">:")))
	    (if(/= temp nil)(setq flag_back_model temp))
;;;	    (file_dat_r_g_w "w" "flag_back_model" flag_back_model)
	    (PL:SetD "flag_back_model" flag_back_model)
	    )
	   ((eq flag_nastr "Тип")
	    (initget "Авто ГОСТ Большой Маленький Нет")(setq temp (getkword (strcat"\nТип вставляемого штампа[Авто/ГОСТ/Большой/Маленький/Нет]<"flag_shtamp">:")))
	    (if(/= temp nil)(setq flag_shtamp temp))
;;;	    (file_dat_r_g_w "w" "shtamp_type" flag_shtamp)
	    (PL:SetD "last_shtamp_type" flag_shtamp)
	    )))
       )
      ((eq name_layout "")
       (setq name_layout (strcat name(itoa num))))
      )
    )
  (setq name_layout(extract_name_num name_layout T))
;;;  (file_dat_r_g_w "w" "name_layout" name_layout)
  (PL:SetD "last_name_layout" name_layout)
  (setq block (car(entsel "\nВыбери блок вспомогательной рамки: ")))
  (if (/= block nil)
    (if (eq (cdr (assoc 0 (entget block)))"INSERT")
      (if (eq (car(vl-string->list (cdr (assoc 2 (entget block)))))95)
	(setq flag nil)
	(setq flag t))
      (setq flag t))
    (exit)
    )
  (if flag
    (alert "Выбран не допустимый объект!")
    (progn
      (setq block_name (cdr (assoc 2 (entget block))))
      
      (setq Scale (cdr (assoc 41 (entget block))))
      (setq Rotation (cdr (assoc 50 (entget block))))
      (setq block_name (vl-list->string(cdr(vl-string->list block_name))))
      (if (eq flag_shtamp "Авто")
	(progn
	  (if(eq (substr block_name 1 3) "ISO")
	    (setq symbol (substr block_name 4 1))
	    (setq symbol (substr block_name 5 1))
	    )
	  (cond
	    ((eq symbol "н")(setq flag_shtamp "Нет"))
	    ((eq symbol "б")(setq flag_shtamp "Большой"))
	    ((eq symbol "м")(setq flag_shtamp "Маленький"))
	    ((eq symbol " ")(setq flag_shtamp "ГОСТ"))
	    )
	  )
	)
      (setq block_name_list (analisis_block_name_for_width_height block_name))
      (setq width (car block_name_list))
      (setq height (cadr block_name_list))
      (if (eq (vla-get-LayoutCreateViewport(vla-get-Display(vla-get-Preferences acad_application))) :vlax-true)
	(vla-put-LayoutCreateViewport(vla-get-Display(vla-get-Preferences acad_application)) :vlax-false))
      (setq new_layout (vla-add layouts_document name_layout))
      (vla-put-ActiveLayout active_document new_layout)
      (setq paper_spece (vla-get-PaperSpace active_document))
      (setq viewport (vla-AddPViewport paper_spece (vlax-3D-point (list (/ width 2) (/ height 2) 0)) width height))
      (vla-put-CustomScale viewport (/ 1 Scale))
      (vla-put-Layer viewport "Вид экрана")
      (setq block_center (polar(polar (cdr (assoc 10 (entget block))) Rotation (* (/ width 2) Scale))
			       (+ Rotation (/ pi 2)) (* (/ height 2) Scale)))
      (setq viewport_center_in_model (cdr (assoc 12 (entget (vlax-vla-object->ename viewport)))))
      (setq target_p_usl_z_m_mg (polar(polar block_center (- Rotation (/ pi 2)) (cadr viewport_center_in_model))
				      (- Rotation pi) (car viewport_center_in_model)))
      (vla-put-Target viewport (vlax-3D-point target_p_usl_z_m_mg))
      (vla-put-TwistAngle viewport (-(* pi 2)Rotation))
      (vla-put-ViewportOn viewport :vlax-true)
      (setq block_name(draw_blok_ramka block_name "Да" nil T nil))
;;;      (princ block_name)
      (entmake (list '(0 . "INSERT") (cons 2 block_name) (cons 10 (list 0 0 0)) '(41 . 1) '(42 . 1) '(43 . 1) '(50 . 0)));'(8 . "Формат")
      (cond
	((eq flag_shtamp "Нет")
	 (entmake (list '(0 . "LWPOLYLINE") '(8 . "Вид экрана")'(100 . "AcDbEntity") '(6 . "Continuous") '(100 . "AcDbPolyline")
			'(90 . 4) '(70 . 1) '(43 . 0)
			(cons 10 (list 25 10 0))
			(cons 10 (list 25 (- height 10) 0))
			(cons 10 (list (- width 10) (- height 10) 0))
			(cons 10 (list (- width 10) 10 0))
			)))
	((eq flag_shtamp "ГОСТ")
	 (entmake (list '(0 . "LWPOLYLINE") '(8 . "Вид экрана")'(100 . "AcDbEntity") '(6 . "Continuous") '(100 . "AcDbPolyline")
			'(90 . 6) '(70 . 1) '(43 . 0)
			(cons 10 (list 25 10 0))
			(cons 10 (list 25 (- height 10) 0))
			(cons 10 (list (- width 10) (- height 10) 0))
			(cons 10 (list (- width 10) 65 0))
			(cons 10 (list (- width 195) 65 0))
			(cons 10 (list (- width 195) 10 0))
			)))
	((eq flag_shtamp "Большой")
	 (entmake (list '(0 . "LWPOLYLINE") '(8 . "Вид экрана")'(100 . "AcDbEntity") '(6 . "Continuous") '(100 . "AcDbPolyline")
			'(90 . 6) '(70 . 1) '(43 . 0)
			(cons 10 (list 25 10 0))
			(cons 10 (list 25 (- height 10) 0))
			(cons 10 (list (- width 10) (- height 10) 0))
			(cons 10 (list (- width 10) 140 0))
			(cons 10 (list (- width 140) 140 0))
			(cons 10 (list (- width 140) 10 0))
			)))
	((eq flag_shtamp "Маленький")
	 (entmake (list '(0 . "LWPOLYLINE") '(8 . "Вид экрана")'(100 . "AcDbEntity") '(6 . "Continuous") '(100 . "AcDbPolyline")
			'(90 . 6) '(70 . 1) '(43 . 0)
			(cons 10 (list 25 10 0))
			(cons 10 (list 25 (- height 10) 0))
			(cons 10 (list (- width 10) (- height 10) 0))
			(cons 10 (list (- width 10) 25 0))
			(cons 10 (list (- width 140) 25 0))
			(cons 10 (list (- width 140) 10 0))
			)))
	)
      (command "_vpclip" (vlax-vla-object->ename viewport) (entlast))
      (if
	(/= flag_shtamp "Нет")
	(progn
	  (draw_blok_shtamp flag_shtamp "Да")
	  (entmake (list '(0 . "INSERT") '(2 . "Штамп") (cons 10 (list(- width 5.0) 5.0 0.0)) '(41 . 1.0) '(42 . 1.0) '(43 . 1.0)))
	  (command "_explode" (entlast))
	  (if(tblobjname "block" "Штамп")(command "_-purge" "Б" "Штамп" "Н"))
	  )
	)
      (if
	(eq flag_back_model "Да")
	(command "рлист" "" "модель")
	)
      
      )
    )
  (princ)
  )
