怎样获取CAD绘图区域左上角的屏幕坐标
本帖最后由 guohq 于 2016-1-22 15:18 编辑通过 PointToScreen 只能获取某点相对于绘图区域 左上角屏幕坐标,要想获取此点在屏幕上的绝对坐标,还必须获取左上角的屏幕坐标。
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 - view_pt.X
y_offset = windowpt - 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 :
screenpt = ed.PointToScreen(pt, 0)
windowpt = Point(int(screenpt.X + x_offset), int(screenpt.Y + y_offset))
winpts.append(windowpt)
screenshot = capture_screen(winpts, winpts)
mat = BitmapConverter.ToMat(screenshot)
prinf(mat)
Cv2.ImShow("img", mat) 本帖最后由 xgr 于 2019-5-30 14:00 编辑
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application;
namespace 取得CAD绘图区域屏幕坐标
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.DLL", EntryPoint = "GetTopWindow", SetLastError = true, CharSet = CharSet.Unicode,
ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr GetTopWindow(IntPtr hWnd);
public static extern IntPtr GetWindow(IntPtr hWnd, UInt32 uCmd);
static extern bool GetWindowRect(IntPtr hWnd, ref Rect lpRect);
public struct Rect
{
public int Left; //最左坐标
public int Top; //最上坐标
public int Right; //最右坐标
public int Bottom; //最下坐标
}
private void button1_Click(object sender, EventArgs e)
{
Rect rect = new Rect();
IntPtr intPtr1 = GetTopWindow(Application.DocumentManager.MdiActiveDocument.Window.Handle);
textBox1.Text = intPtr1.ToString();
IntPtr intPtr2 = GetWindow(intPtr1, 2);
GetWindowRect(intPtr2, ref rect);
textBox2.Text = rect.Left.ToString();
textBox3.Text = rect.Top.ToString();
textBox4.Text = (rect.Right - rect.Left).ToString();
textBox5.Text = (rect.Bottom - rect.Top).ToString();
}
}
}
本帖最后由 d1742647821 于 2023-12-26 09:19 编辑
可以参考一下
CAD坐标转为屏幕坐标
http://bbs.mjtd.com/forum.php?mod=viewthread&tid=189169&fromuid=7329732
(出处: 明经CAD社区)
读取系统变量"VIEWSIZE" "SCREENSIZE" "VIEWCTR"计算 此方法不行 同步学习中…………谢谢 本帖最后由 ivde 于 2016-1-23 22:03 编辑
CPoint pt(0,0);
acedDwgPoint ptOut;
acedCoordFromPixelToWorld(pt,ptOut); ivde 发表于 2016-1-23 21:56 static/image/common/back.gif
CPoint pt(0,0);
acedDwgPoint ptOut;
acedCoordFromPixelToWorld(pt,ptOut);
不好意思 ,我在.net 中没有找到此方法呢acedCoordFromPixelToWorld,能说详细点不?谢谢!!
private static extern bool acedCoordFromWorldToPixel(int viewportNumber, ref Point3d worldPt, out Point pixel);
经查证,acedCoordFromPixelToWorld 与 Editor.PointToScreen 方法得到的结果一样。 var pt= doc.Window.Location;
var size = doc.Window.Size;
var gs = doc.GraphicsManager;
var gsSize = doc.GraphicsManager.DisplaySize;
pt.Offset(0, gsSize.Height - size.Height); 我也想知道 鱼与熊掌 发表于 2016-1-25 14:39 static/image/common/back.gif
谢谢,此方法还是不够精确
页:
[1]
2