from flask import Flask, request
import os
import pyautocad
app = Flask(__name__)
@app.route('/insert_signature', methods=['POST'])
def insert_signature():
# 从请求中获取DWG文件路径和签名块名称和插入点
data = request.get_json()
dwg_file_path = data['dwg_file_path']
signature_block_name = data['signature_block_name']
signature_insertion_point = tuple(data['signature_insertion_point'])
# 创建AutoCAD对象
acad = pyautocad.Autocad()
# 打开指定的DWG文件
doc = acad.Application.Documents.Open(dwg_file_path)
# 导入图片并创建块对象
signature_image_path = os.path.join(os.path.dirname(__file__), 'signature.png')
signature_block_points = [(0, 0), (50, 0), (50, 10), (0, 10)]
signature_block = acad.model.AddRaster(signature_image_path, signature_insertion_point, scale_x=1, scale_y=1)
signature_block.name = signature_block_name
# 查找之前的电子签章并删除
for entity in acad.iter_objects(['AcDbBlockReference']):
if entity.Name == signature_block_name and entity.InsertionPoint == signature_insertion_point:
entity.Delete()
# 插入新的电子签章
acad.model.InsertBlock(signature_insertion_point, signature_block_name, 1, 1, 1, 0)
# 保存文档并关闭AutoCAD应用程序
doc.Save()
doc.Close()
return 'Signature inserted successfully.'
if __name__ == '__main__':
app.run(debug=True)
|