如何获取 dm-script 行注释(组件)的 start/end 点?

How to get start/end point of a dm-script line annotation (Component)?

我想使用:

Component NewLineAnnotation( Number top, Number left, Number bottom, Number right )

问题是这条线总是从左上角到右下角的方向。

但是,我需要从右下角到左上角的线条。 而且我还需要能够从现有的方向上获得准确的方向。

看起来很傻,命令不是:
Component NewLineAnnotation( Number start_x, Number start_y, Number end_x, Number end_y )

但肯定有办法得到线注释的方向吗?

void ComponentGetRect( Component comp, NumberVariable top, NumberVariable left, NumberVariable bottom, NumberVariable right )

没有这样的细节。

这是一个有趣的问题! 我认为 ComponentGetRect() 的行为是正确的,但错误地标记了文档中的变量名称。虽然它说 returns 你是一个 top-left 到 bottom-right 坐标的矩形,但实际上 returns 你是 Y/X 起始坐标和 Y/X注释的结束坐标对。 (对于其他类型的注释,则为 [TLBR] )。注意是Y/X不是X/Y!这令人困惑,但为了使其与更通用的“组件”对象保持一致。

同样,NewLineAnnotation() 的签名变量名称是错误的,因为它们很明显地给出了从开始到结束的方向,因为您可以在使用 arrow-annotations 时轻松尝试。 (请参阅下面的示例脚本。)

You can defenitly set the direction of a created line or arrow annotation using:

Component NewLineAnnotation( Number StartY, Number StartX, Number EndY, Number EndX )
Component NewArrowAnnotation( Number StartY, Number StartX, Number EndY, Number EndX )

Similarly, you should use for reading the postions:
ComponentGetRect( NumberVariable StartY, NumberVariable StartX, NumberVariable EndY, NumberVariable EndX )

现在,另一种获取开始和结束坐标的方法是组件的 控制点 ,F1 帮助文档明确列出了控制点 ID 值:

因此,大概正确的方法是使用以下方法向组件询问指定 控制点 的坐标:

Boolean ComponentGetControlPoint( Component comp, Number loc, NumberVariable x, NumberVariable y )

但是,对其进行测试(参见下面的脚本)它实际上不适用于 控制点 ID 值 12。好像一条线的两个控制点start/end没有用命令实现

但是,您可以使用“Top/Left”控制点 4获取起始值和“Bottom/Right”控制点 7获取最终值。同样,这仅匹配常规组件上“line/arrow”注释的相同的不规则映射。

Alternatvive way of getting a start point XY:
Boolean ComponentGetControlPoint( Component comp, 4, NumberVariable x, NumberVariable y )

Alternatvive way of getting a end point XY:
Boolean ComponentGetControlPoint( Component comp, 7, NumberVariable x, NumberVariable y )


使用的测试脚本

// Control Point constants. See F1 help documentaton ("Component Object" Overview)
number kLineStart   = 1 // = start point of line
number kLineEnd     = 2 // = end point of line
number kTopLeft     = 4 // = top-left for rect, or start point for line
number kBottomRight = 7 // = bottom-right for rect, or end point for line

number sx = 50
number sy = 30
number ex = 400
number ey = 200
number sizeX = 500
number sizeY = 500

void PrintAnnoInfo( component anno )
{
    if ( !anno.ComponentIsValid() ) return
    number t,l,b,r
    anno.ComponentGetRect( t, l, b, r ) 
    // note that it is return StartY, StartX, EndY, EndX into TLBR!
    
    number type = anno.ComponentGetType()
    Result( "\n\n Component of type " + type + ":" )
    Result( "\n\t Position rect: " + t + " / " + l + " / "+ b + " / "+ r )
    Result( "\n\t\t { TOP / LEFT / BOTTOM / RIGHT }" )
    
    number sx, sy, ex, ey
    if ( !anno.ComponentGetControlPoint( kLineStart, sx, sy ) )
        Result( "\n INVALID Control point for annotation type. ( " + kLineStart + " ) " )
    else
        Result( "\n\t Start point (X/Y): " + sx + " / " + sy )
    if ( !anno.ComponentGetControlPoint( kLineEnd, ex, ey ) )
        Result( "\n INVALID Control point for annotation type. ( " + kLineEnd + " ) " )
    else
        Result( "\n\t End point (X/Y)  : " + ex + " / " + ey )

    if ( !anno.ComponentGetControlPoint( kTopLeft, sx, sy ) )
        Result( "\n INVALID Control point for annotation type. ( " + kLineStart + " ) " )
    else
        Result( "\n\t Start point (X/Y): " + sx + " / " + sy )
    if ( !anno.ComponentGetControlPoint( kBottomRight, ex, ey ) )
        Result( "\n INVALID Control point for annotation type. ( " + kLineEnd + " ) " )
    else
        Result( "\n\t End point (X/Y)  : " + ex + " / " + ey )
    
Result( "\n\n" )
    
}

// clear workspace and created dummy
EGUPerformActionWithAllShownImages( "Delete" )
ClearResults()
Result( "Component direction test:\n\n" )
image img := Realimage( "Test", 4, sizeX, sizeY )
img.ShowImage()
imageDisplay disp = img.ImageGetImageDisplay(0)

// Creating and adding line-Annotation
number UseArrow
useArrow = TwoButtonDialog( "In this test use", "Arrows", "Lines" )
string msg
msg = "\nAdding " + (useArrow?"arrow":"line") + ":"
msg += "\n from (" + sx + " / " + sy + ") to (" + ex + " / " + ey + ")"
msg += "\n { from (Ex/Ey) to (Sx/Sy) }"
Result( msg )
OKdialog( msg )

Component anno
if ( useArrow )
    anno = NewArrowAnnotation( sy, sx, ey, ex ) // note that it is YX!
else
    anno = NewLineAnnotation( sy, sx, ey, ex )  // note that it is YX!

anno.ComponentSetForegroundcolor(1,0,0)
disp.ComponentAddChildAtEnd( anno )
anno.PrintAnnoInfo()

msg = "\nAdding " + (useArrow?"arrow":"line") + ":"
msg += "\n from (" + ex + " / " + ey + ") to (" + sx + " / " + sy + ")"
msg += "\n { from (Ex/Ey) to (Sx/Sy) }"
Result( msg )
OKdialog( msg )

if ( useArrow )
    anno = NewArrowAnnotation( ey, ex, sy, sx ) // note that it is YX!
else
    anno = NewLineAnnotation( ey, ex, sy, sx )  // note that it is YX!


anno.ComponentSetForegroundcolor(0,1,0)
disp.ComponentAddChildAtBeginning( anno )
anno.PrintAnnoInfo()

根据之前的精彩回答中的详细信息,我查看了更多,发现情况就是这样 - 我正在使用 GMS 3。

通过以下方式创建线作为组件的命令:

NewLineAnnotation( Number top, Number left, Number bottom, Number right )

可以更好地描述为:

NewLineAnnotation( Number sX, Number sY, Number eX, Number eY )

但是还有另一个潜在的混淆来源。在查找坐标的 Get() 命令时,列出了这些命令:

void ComponentGetBoundingRect( Component comp, NumberVariable t, NumberVariable l, NumberVariable b, NumberVariable r )

void ComponentGetRect( Component comp, NumberVariable top, NumberVariable left, NumberVariable bottom, NumberVariable right ) 

void ComponentGetRectInView( Component comp, NumberVariable top, NumberVariable left, NumberVariable bottom, NumberVariable right )

所有 3 个命令都声明了顺序:上、左、下、右,但只有第一个和最后一个命令符合。

可以再次更好地描述第二个命令
ComponentGetRect(Number sX, Number sY, Number eX, Number eY)

我将命令 NewLineAnnotation() 与 ComponentGetBoundingRect() 混淆了,这导致了一些实际问题。我觉得手册中更好的描述可能有助于避免此类问题。

总而言之,这是我用来向自己演示不同功能的一些代码。

image front := CreateFloatImage("", 800, 800)
front.setZoom(.4)
front.ShowImage()
component frontComponent = front.ImageGetImageDisplay(0)

component solid_Line_orange = NewLineAnnotation(700, 10, 500, 200);
frontComponent.ComponentAddChildAtEnd(solid_Line_orange)
solid_Line_orange.ComponentSetForegroundColor(1,.66,0);
component solid_Line_white  = NewLineAnnotation(500, 10, 700, 200);
frontComponent.ComponentAddChildAtEnd(solid_Line_white)

number sx, sy, ex, ey
number l_top, l_left, l_buttom, l_right
solid_Line_orange.ComponentGetRect( l_top, l_left, l_buttom, l_right );

Result("\n\n")
Result("using:  ComponentGetRectangle();\t actual convention: (start_x, start_y, end_x, end_y)\n")
Result("orange line set at: \t(700, 10, 500, 200);\n")
Result("orange line found at: \t(" + l_top + ", " +  l_left + ", " + l_buttom + ", " + l_right + ")\n")
solid_Line_white.ComponentGetRect( l_top, l_left, l_buttom, l_right ) 
Result("white line set at: \t(500, 10, 700, 200); \n")
Result("white line found at: \t(" + l_top + ", " +  l_left + ", " + l_buttom + ", " + l_right + ")\n")

Result("using:  ComponentGetBoundingRectangle();\t actual convention: (top, left, bottom, right)\n")
solid_Line_orange.ComponentGetBoundingRect( l_top, l_left, l_buttom, l_right ); 
Result("orange line found at: \t(" + l_top + ", " +  l_left + ", " + l_buttom + ", " + l_right + ")\n")
solid_Line_white.ComponentGetBoundingRect( l_top, l_left, l_buttom, l_right ); 
Result("white line found at: \t(" + l_top + ", " +  l_left + ", " + l_buttom + ", " + l_right + ")\n")

Result("using:  ComponentGetBoundingRectInView();\t actual convention: (top, left, bottom, right)\n")
solid_Line_orange.ComponentGetBoundingRectInView( l_top, l_left, l_buttom, l_right ); 
Result("orange line found at: \t(" + l_top + ", " +  l_left + ", " + l_buttom + ", " + l_right + ")\n")
solid_Line_white.ComponentGetBoundingRectInView( l_top, l_left, l_buttom, l_right ); 
Result("white line found at: \t(" + l_top + ", " +  l_left + ", " + l_buttom + ", " + l_right + ")\n")

这解决了我的问题。希望这也能帮助其他人。也许手册中更好的描述也会有所帮助。