forked from mcneel/rhino-developer-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSampleMove.py
84 lines (69 loc) · 2.62 KB
/
SampleMove.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
################################################################################
# SampleMove.py
# Copyright (c) 2017 Robert McNeel & Associates.
# See License.md in the root of this repository for details.
################################################################################
from Rhino import *
from Rhino.Commands import *
from Rhino.Geometry import *
from Rhino.Input.Custom import *
from scriptcontext import doc
# Rhino.Input.Custom.GetPoint derived class that dynamically
# draws the selected objects.
class SampleGetMovePoint(GetPoint):
# Class initializer
def __init__(self, go, base_point):
self.m_objects = [o.Object() for o in go.Objects()]
self.m_point = base_point
self.m_xform = Transform.Identity
# Calculates the transformation
def CalculateTransform(self, point):
xform = Transform.Identity
dir = point - self.m_point
if (not dir.IsTiny()):
xform = Transform.Translation(dir)
return xform
# OnMouseMove override
def OnMouseMove(self, e):
self.m_xform = self.CalculateTransform(e.Point)
GetPoint.OnMouseMove(self, e)
# OnDynamicDraw override
def OnDynamicDraw(self, e):
for obj in self.m_objects:
e.Display.DrawObject(obj, self.m_xform)
GetPoint.OnDynamicDraw(self, e)
# Moves objects from one location to another.
def SampleMove():
# Select objects to move
go = GetObject()
go.SetCommandPrompt("Select objects to move")
go.GetMultiple(1, 0)
if go.CommandResult() <> Result.Success:
return go.CommandResult()
# Point to move from
gp = GetPoint()
gp.SetCommandPrompt("Point to move from")
gp.Get()
if gp.CommandResult() <> Result.Success:
return gp.CommandResult()
base_point = gp.Point()
# Point to move to
gm = SampleGetMovePoint(go, base_point)
gm.SetCommandPrompt("Point to move to")
gm.SetBasePoint(base_point, True)
gm.DrawLineFromPoint(base_point, True)
gm.Get()
if gm.CommandResult() <> Result.Success:
return gm.CommandResult()
target_point = gm.Point()
# One final transformation calculation
xform = gm.CalculateTransform(target_point);
# Transform (move) the objects
for o in go.Objects():
doc.Objects.Transform(o, xform, True);
doc.Views.Redraw()
# Check to see if this file is being executed as the "main" python
# script instead of being used as a module by some other python script
# This allows us to use the module which ever way we want.
if __name__ == "__main__":
SampleMove()