- 积分
- 1905
- 明经币
- 个
- 注册时间
- 2022-4-4
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
发表于 2023-12-23 21:00:11
|
显示全部楼层
- from pycad.system import *
- from pycad.runtime import *
- import ctypes
- import clr
- clr.AddReference("System.Drawing")
- clr.AddReference("System.Windows.Forms")
- clr.AddReference("PresentationCore")
- clr.AddReference("PresentationFramework")
- clr.AddReference("WindowsBase")
- clr.AddReference("OpenCvSharp")
- clr.AddReference("OpenCvSharp.Extensions")
- import OpenCvSharp as cv
- from OpenCvSharp import Cv2
- from OpenCvSharp.Extensions import BitmapConverter
- from System.Windows.Forms import Form, Control, NativeWindow
- from System.Drawing import Point, Bitmap, Graphics
- from System.Windows.Forms import Screen
- class POINT(ctypes.Structure):
- _fields_ = [("x", ctypes.c_long), ("y", ctypes.c_long)]
- def get_mouse_position():
- pt = POINT()
- ctypes.windll.user32.GetCursorPos(ctypes.byref(pt))
- return pt.x, pt.y
- x_offset = 0
- y_offset = 0
- @Command()
- def InitialScreen(doc: Document):
- """屏幕坐标校准"""
- global x_offset, y_offset
- ed = doc.Editor
- res = getpoint()
- if not res.ok:
- return
- cad_pt = res.value # tpye:Point3d
- windowpt = get_mouse_position() # 屏幕的绝对坐标
- view_pt = ed.PointToScreen(cad_pt, 0) # CAD视口的相对坐标
- # 求出坐标偏移值
- x_offset = windowpt[0] - view_pt.X
- y_offset = windowpt[1] - view_pt.Y
- def capture_screen(pt1: Point, pt2: Point):
- """截屏"""
- x1, x2, y1, y2 = int(pt1.X), int(pt2.X), int(pt1.Y), int(pt2.Y)
- xmin = min(x1, x2) - 1
- xmax = max(x1, x2) + 1
- ymin = min(y1, y2) - 1
- ymax = max(y1, y2) + 1
- width = xmax - xmin
- height = ymax - ymin
- bitmap = Bitmap(width, height)
- graphics = Graphics.FromImage(bitmap)
- graphics.CopyFromScreen(xmin, ymin, 0, 0, bitmap.Size)
- return bitmap
- @Command()
- def CaptureScreen(doc: Document):
- global x_offset, y_offset
- ed = doc.Editor
- res = getpoint()
- if not res.ok:
- return
- pt1 = res.value
- res = getcorner(basept=pt1)
- if not res.ok:
- return
- pt2 = res.value
- winpts = []
- for pt in [pt1, pt2]:
- screenpt = ed.PointToScreen(pt, 0)
- windowpt = Point(int(screenpt.X + x_offset), int(screenpt.Y + y_offset))
- winpts.append(windowpt)
- screenshot = capture_screen(winpts[0], winpts[1])
- mat = BitmapConverter.ToMat(screenshot)
- prinf(mat)
- Cv2.ImShow("img", mat)
复制代码 |
|