This is page 6 of 8. Use http://codebase.md/jingcheng-chen/rhinomcp?lines=true&page={x} to view the full context.
# Directory Structure
```
├── .github
│ └── workflows
│ ├── mcp-server-publish.yml
│ └── rhino-plugin-publish.yml
├── .gitignore
├── assets
│ ├── claude_enable_instruction.jpg
│ ├── cursor_enable_instruction.jpg
│ ├── cursor_usage_instruction.jpg
│ ├── demo1.jpg
│ ├── demo2.jpg
│ ├── rhino_plugin_instruction.jpg
│ └── rhinomcp_logo.svg
├── demo_chats
│ ├── create_6x6x6_boxes.txt
│ └── create_rhinoceros_lego_blocks.txt
├── LICENSE
├── README.md
├── rhino_mcp_plugin
│ ├── .gitignore
│ ├── Commands
│ │ ├── MCPStartCommand.cs
│ │ ├── MCPStopCommand.cs
│ │ └── MCPVersionCommand.cs
│ ├── EmbeddedResources
│ │ └── plugin-utility.ico
│ ├── Functions
│ │ ├── _utils.cs
│ │ ├── CreateLayer.cs
│ │ ├── CreateObject.cs
│ │ ├── CreateObjects.cs
│ │ ├── DeleteLayer.cs
│ │ ├── DeleteObject.cs
│ │ ├── ExecuteRhinoscript.cs
│ │ ├── GetDocumentInfo.cs
│ │ ├── GetObjectInfo.cs
│ │ ├── GetOrSetCurrentLayer.cs
│ │ ├── GetSelectedObjectsInfo.cs
│ │ ├── ModifyObject.cs
│ │ ├── ModifyObjects.cs
│ │ └── SelectObjects.cs
│ ├── manifest.yml
│ ├── Properties
│ │ ├── AssemblyInfo.cs
│ │ └── launchSettings.json
│ ├── rhinomcp.csproj
│ ├── rhinomcp.sln
│ ├── RhinoMCPPlugin.cs
│ ├── RhinoMCPServer.cs
│ ├── RhinoMCPServerController.cs
│ └── Serializers
│ └── Serializer.cs
└── rhino_mcp_server
├── .gitignore
├── dev.sh
├── main.py
├── pyproject.toml
├── README.md
├── src
│ └── rhinomcp
│ ├── __init__.py
│ ├── prompts
│ │ └── assert_general_strategy.py
│ ├── server.py
│ ├── static
│ │ └── rhinoscriptsyntax.py
│ └── tools
│ ├── create_layer.py
│ ├── create_object.py
│ ├── create_objects.py
│ ├── delete_layer.py
│ ├── delete_object.py
│ ├── execute_rhinoscript_python_code.py
│ ├── get_document_info.py
│ ├── get_object_info.py
│ ├── get_or_set_current_layer.py
│ ├── get_rhinoscript_python_code_guide.py
│ ├── get_rhinoscript_python_function_names.py
│ ├── get_selected_objects_info.py
│ ├── modify_object.py
│ ├── modify_objects.py
│ └── select_objects.py
├── static
│ ├── application.py
│ ├── block.py
│ ├── compat.py
│ ├── curve.py
│ ├── dimension.py
│ ├── document.py
│ ├── geometry.py
│ ├── grips.py
│ ├── group.py
│ ├── hatch.py
│ ├── layer.py
│ ├── light.py
│ ├── line.py
│ ├── linetype.py
│ ├── material.py
│ ├── mesh.py
│ ├── object.py
│ ├── plane.py
│ ├── pointvector.py
│ ├── selection.py
│ ├── surface.py
│ ├── toolbar.py
│ ├── transformation.py
│ ├── userdata.py
│ ├── userinterface.py
│ ├── utility.py
│ └── view.py
└── uv.lock
```
# Files
--------------------------------------------------------------------------------
/rhino_mcp_server/static/view.py:
--------------------------------------------------------------------------------
```python
1 | import math
2 |
3 | import System
4 |
5 | import Rhino
6 |
7 | import scriptcontext
8 |
9 | from rhinoscript import utility as rhutil
10 |
11 |
12 | def __viewhelper(view):
13 | if view is None: return scriptcontext.doc.Views.ActiveView
14 | allviews = scriptcontext.doc.Views.GetViewList(True, True)
15 | view_id = rhutil.coerceguid(view, False)
16 | for item in allviews:
17 | if view_id:
18 | if item.MainViewport.Id == view_id: return item
19 | elif item.MainViewport.Name == view:
20 | return item
21 | raise ValueError("unable to coerce %s into a view"%view)
22 |
23 |
24 | def AddDetail(layout_id, corner1, corner2, title=None, projection=1):
25 | """Add new detail view to an existing layout view
26 | Parameters:
27 | layout_id (guid): identifier of an existing layout
28 | corner1, corner2 (point): 2d corners of the detail in the layout's unit system
29 | title (str, optional): title of the new detail
30 | projection (number, optional): type of initial view projection for the detail
31 | 1 = parallel top view
32 | 2 = parallel bottom view
33 | 3 = parallel left view
34 | 4 = parallel right view
35 | 5 = parallel front view
36 | 6 = parallel back view
37 | 7 = perspective view
38 | Returns:
39 | guid: identifier of the newly created detail on success
40 | None: on error
41 | Example:
42 | import rhinoscriptsyntax as rs
43 | layout = rs.AddLayout("Portrait", (8.5,11))
44 | if layout:
45 | rs.AddDetail(layout, (0.5,0.5), (8,10.5), None, 7)
46 | See Also:
47 | DeleteNamedView
48 | NamedViews
49 | RestoreNamedView
50 | """
51 | layout_id = rhutil.coerceguid(layout_id, True)
52 | corner1 = rhutil.coerce2dpoint(corner1, True)
53 | corner2 = rhutil.coerce2dpoint(corner2, True)
54 | if projection<1 or projection>7: raise ValueError("projection must be a value between 1-7")
55 | layout = scriptcontext.doc.Views.Find(layout_id)
56 | if not layout: raise ValueError("no layout found for given layout_id")
57 | projection = System.Enum.ToObject(Rhino.Display.DefinedViewportProjection, projection)
58 | detail = layout.AddDetailView(title, corner1, corner2, projection)
59 | if not detail: return scriptcontext.errorhandler()
60 | scriptcontext.doc.Views.Redraw()
61 | return detail.Id
62 |
63 |
64 | def AddLayout(title=None, size=None):
65 | """Adds a new page layout view
66 | Parameters:
67 | title (str, optional): title of new layout
68 | size ([number, number], optional): width and height of paper for the new layout
69 | Returns:
70 | guid: id of new layout
71 | Example:
72 | import rhinoscriptsyntax as rs
73 | rs.AddLayout("Portrait")
74 | See Also:
75 | DeleteNamedView
76 | NamedViews
77 | RestoreNamedView
78 | """
79 | page = None
80 | if size is None: page = scriptcontext.doc.Views.AddPageView(title)
81 | else: page = scriptcontext.doc.Views.AddPageView(title, size[0], size[1])
82 | if page: return page.MainViewport.Id
83 |
84 |
85 | def AddNamedCPlane(cplane_name, view=None):
86 | """Adds new named construction plane to the document
87 | Parameters:
88 | cplane_name (str): the name of the new named construction plane
89 | view (guid|str): Title or identifier of the view from which to save
90 | the construction plane. If omitted, the current active view is used.
91 | Returns:
92 | atr: name of the newly created construction plane if successful
93 | None: on error
94 | Example:
95 | import rhinoscriptsyntax as rs
96 | views = rs.ViewNames()
97 | if views:
98 | for view in views:
99 | name = view + "_cplane"
100 | rs.AddNamedCPlane( name, view )
101 | See Also:
102 | DeleteNamedCPlane
103 | NamedCPlane
104 | NamedCPlanes
105 | RestoreNamedCPlane
106 | """
107 | view = __viewhelper(view)
108 | if not cplane_name: raise ValueError("cplane_name is empty")
109 | plane = view.MainViewport.ConstructionPlane()
110 | index = scriptcontext.doc.NamedConstructionPlanes.Add(cplane_name, plane)
111 | if index<0: return scriptcontext.errorhandler()
112 | return cplane_name
113 |
114 |
115 | def AddNamedView(name, view=None):
116 | """Adds a new named view to the document
117 | Parameters:
118 | name (str): the name of the new named view
119 | view: (guid|str): the title or identifier of the view to save. If omitted, the current
120 | active view is saved
121 | Returns:
122 | str: name fo the newly created named view if successful
123 | None: on error
124 | Example:
125 | import rhinoscriptsyntax as rs
126 | views = rs.ViewNames()
127 | if views:
128 | for view in views:
129 | name = view + "_view"
130 | rs.AddNamedView( name, view )
131 | See Also:
132 | DeleteNamedView
133 | NamedViews
134 | RestoreNamedView
135 | """
136 | view = __viewhelper(view)
137 | if not name: raise ValueError("name is empty")
138 | viewportId = view.MainViewport.Id
139 | index = scriptcontext.doc.NamedViews.Add(name, viewportId)
140 | if index<0: return scriptcontext.errorhandler()
141 | return name
142 |
143 |
144 | def CurrentDetail(layout, detail=None, return_name=True):
145 | """Returns or changes the current detail view in a page layout view
146 | Parameters:
147 | layout (str|guid): title or identifier of an existing page layout view
148 | detail (str|guid, optional): title or identifier the the detail view to set
149 | return_name (bool, optional): return title if True, else return identifier
150 | Returns:
151 | str: if detail is not specified, the title or id of the current detail view
152 | str: if detail is specified, the title or id of the previous detail view
153 | None: on error
154 | Example:
155 | import rhinoscriptsyntax as rs
156 | layout = rs.CurrentView(return_name=False)
157 | if rs.IsLayout(layout):
158 | rs.CurrentDetail( layout, layout )
159 | See Also:
160 | IsDetail
161 | IsLayout
162 | """
163 | layout_id = rhutil.coerceguid(layout)
164 | page = None
165 | if layout_id is None: page = scriptcontext.doc.Views.Find(layout, False)
166 | else: page = scriptcontext.doc.Views.Find(layout_id)
167 | if page is None: return scriptcontext.errorhandler()
168 | rc = None
169 | active_viewport = page.ActiveViewport
170 | if return_name: rc = active_viewport.Name
171 | else: rc = active_viewport.Id
172 | if detail:
173 | id = rhutil.coerceguid(detail)
174 | if( (id and id==page.MainViewport.Id) or (id is None and detail==page.MainViewport.Name) ):
175 | page.SetPageAsActive()
176 | else:
177 | if id: page.SetActiveDetail(id)
178 | else: page.SetActiveDetail(detail, False)
179 | scriptcontext.doc.Views.Redraw()
180 | return rc
181 |
182 |
183 | def CurrentView(view=None, return_name=True):
184 | """Returns or sets the currently active view
185 | Parameters:
186 | view (str|guid): Title or id of the view to set current.
187 | If omitted, only the title or identifier of the current view is returned
188 | return_name (bool, optional): If True, then the name, or title, of the view is returned.
189 | If False, then the identifier of the view is returned
190 | Returns:
191 | str: if the title is not specified, the title or id of the current view
192 | str: if the title is specified, the title or id of the previous current view
193 | None: on error
194 | Example:
195 | import rhinoscriptsyntax as rs
196 | previous = rs.CurrentView("Perspective")
197 | print("The previous current view was {}".format(previous))
198 | viewId = rs.CurrentView( return_name=False )
199 | print("The identifier of the current view is {}".format(viewId))
200 | See Also:
201 | IsViewCurrent
202 | ViewNames
203 | """
204 | rc = None
205 | if return_name: rc = scriptcontext.doc.Views.ActiveView.MainViewport.Name
206 | else: rc = scriptcontext.doc.Views.ActiveView.MainViewport.Id
207 | if view:
208 | id = rhutil.coerceguid(view)
209 | rhview = None
210 | if id: rhview = scriptcontext.doc.Views.Find(id)
211 | else: rhview = scriptcontext.doc.Views.Find(view, False)
212 | if rhview is None: return scriptcontext.errorhandler()
213 | scriptcontext.doc.Views.ActiveView = rhview
214 | return rc
215 |
216 |
217 | def DeleteNamedCPlane(name):
218 | """Removes a named construction plane from the document
219 | Parameters:
220 | name (str): name of the construction plane to remove
221 | Returns:
222 | bool: True or False indicating success or failure
223 | Example:
224 | import rhinoscriptsyntax as rs
225 | cplanes = rs.NamedCplanes()
226 | if cplanes:
227 | for cplane in cplanes: rs.DeleteNamedCPlane(cplane)
228 | See Also:
229 | AddNamedCPlane
230 | NamedCPlane
231 | NamedCPlanes
232 | RestoreNamedCPlane
233 | """
234 | return scriptcontext.doc.NamedConstructionPlanes.Delete(name)
235 |
236 |
237 | def DeleteNamedView(name):
238 | """Removes a named view from the document
239 | Parameters:
240 | name (str): name of the named view to remove
241 | Returns:
242 | bool: True or False indicating success or failure
243 | Example:
244 | import rhinoscriptsyntax as rs
245 | views = rs.NamedViews()
246 | if views:
247 | for view in views: rs.DeleteNamedView(view)
248 | See Also:
249 | AddNamedView
250 | NamedViews
251 | RestoreNamedView
252 | """
253 | return scriptcontext.doc.NamedViews.Delete(name)
254 |
255 |
256 | def DetailLock(detail_id, lock=None):
257 | """Returns or modifies the projection locked state of a detail
258 | Parameters:
259 | detail_id (guid): identifier of a detail object
260 | lock (bool, optional) the new lock state
261 | Returns:
262 | bool: if lock==None, the current detail projection locked state
263 | bool: if lock is True or False, the previous detail projection locked state
264 | None: on error
265 | Example:
266 | import rhinoscriptsyntax as rs
267 | detail = rs.GetObject("select a detail", rs.filter.detail)
268 | if detail: rs.DetailLock(detail,True)
269 | See Also:
270 | IsDetail
271 | IsLayout
272 | """
273 | detail_id = rhutil.coerceguid(detail_id, True)
274 | detail = scriptcontext.doc.Objects.Find(detail_id)
275 | if not detail: return scriptcontext.errorhandler()
276 | rc = detail.DetailGeometry.IsProjectionLocked
277 | if lock is not None and lock!=rc:
278 | detail.DetailGeometry.IsProjectionLocked = lock
279 | detail.CommitChanges()
280 | return rc
281 |
282 |
283 | def DetailScale(detail_id, model_length=None, page_length=None):
284 | """Returns or modifies the scale of a detail object
285 | Parameters:
286 | detail_id (guid): identifier of a detail object
287 | model_length (number, optional): a length in the current model units
288 | page_length (number, optional): a length in the current page units
289 | Returns:
290 | number: current page to model scale ratio if model_length and page_length are both None
291 | number: previous page to model scale ratio if model_length and page_length are values
292 | None: on error
293 | Example:
294 | import rhinoscriptsyntax as rs
295 | detail = rs.GetObject("select a detail", rs.filter.detail)
296 | if detail: rs.DetailScale(detail,1,1)
297 | See Also:
298 | IsDetail
299 | IsLayout
300 | """
301 | detail_id = rhutil.coerceguid(detail_id, True)
302 | detail = scriptcontext.doc.Objects.Find(detail_id)
303 | if detail is None: return scriptcontext.errorhandler()
304 | rc = detail.DetailGeometry.PageToModelRatio
305 | if model_length or page_length:
306 | if model_length is None or page_length is None:
307 | return scriptcontext.errorhandler()
308 | model_units = scriptcontext.doc.ModelUnitSystem
309 | page_units = scriptcontext.doc.PageUnitSystem
310 | if detail.DetailGeometry.SetScale(model_length, model_units, page_length, page_units):
311 | detail.CommitChanges()
312 | scriptcontext.doc.Views.Redraw()
313 | return rc
314 |
315 |
316 | def IsDetail(layout, detail):
317 | """Verifies that a detail view exists on a page layout view
318 | Parameters:
319 | layout (str|guid): title or identifier of an existing page layout
320 | detail (str|guid): title or identifier of an existing detail view
321 | Returns:
322 | bool: True if detail is a detail view
323 | bool: False if detail is not a detail view
324 | None: on error
325 | Example:
326 | import rhinoscriptsyntax as rs
327 | view = rs.CurrentView()
328 | if rs.IsLayout(view):
329 | isdetail = rs.IsDetail(view, "Top")
330 | if isdetail:
331 | print("Top is a detail view.")
332 | else:
333 | print("Top is not a detail view.")
334 | See Also:
335 | IsLayout
336 | CurrentDetail
337 | """
338 | layout_id = rhutil.coerceguid(layout)
339 | views = scriptcontext.doc.Views.GetViewList(False, True)
340 | found_layout = None
341 | for view in views:
342 | if layout_id:
343 | if view.MainViewport.Id==layout_id:
344 | found_layout = view
345 | break
346 | elif view.MainViewport.Name==layout:
347 | found_layout = view
348 | break
349 | # if we couldn't find a layout, this is an error
350 | if found_layout is None: return scriptcontext.errorhandler()
351 | detail_id = rhutil.coerceguid(detail)
352 | details = view.GetDetailViews()
353 | if not details: return False
354 | for detail_view in details:
355 | if detail_id:
356 | if detail_view.Id==detail_id: return True
357 | else:
358 | if detail_view.Name==detail: return True
359 | return False
360 |
361 |
362 | def IsLayout(layout):
363 | """Verifies that a view is a page layout view
364 | Parameters:
365 | layout (guid|str): title or identifier of an existing page layout view
366 | Returns:
367 | bool: True if layout is a page layout view
368 | bool: False is layout is a standard, model view
369 | None: on error
370 | Example:
371 | import rhinoscriptsyntax as rs
372 | view = rs.CurrentView()
373 | if rs.IsLayout(view):
374 | print("The current view is a page layout view.")
375 | else:
376 | print("The current view is standard, model view.")
377 | See Also:
378 | IsLayout
379 | CurrentDetail
380 | """
381 | layout_id = rhutil.coerceguid(layout)
382 | alllayouts = scriptcontext.doc.Views.GetViewList(False, True)
383 | for layoutview in alllayouts:
384 | if layout_id:
385 | if layoutview.MainViewport.Id==layout_id: return True
386 | elif layoutview.MainViewport.Name==layout: return True
387 | allmodelviews = scriptcontext.doc.Views.GetViewList(True, False)
388 | for modelview in allmodelviews:
389 | if layout_id:
390 | if modelview.MainViewport.Id==layout_id: return False
391 | elif modelview.MainViewport.Name==layout: return False
392 | return scriptcontext.errorhandler()
393 |
394 |
395 | def IsView(view):
396 | """Verifies that the specified view exists
397 | Parameters:
398 | view (str|guid): title or identifier of the view
399 | Returns:
400 | bool: True of False indicating success or failure
401 | Example:
402 | import rhinoscriptsyntax as rs
403 | title = "Perspective"
404 | result = rs.IsView(title)
405 | if result:
406 | print("The {} view exists.".format(title))
407 | else:
408 | print("The {} view does not exist.".format(title))
409 | See Also:
410 | ViewNames
411 | """
412 | view_id = rhutil.coerceguid(view)
413 | if view_id is None and view is None: return False
414 | allviews = scriptcontext.doc.Views.GetViewList(True, True)
415 | for item in allviews:
416 | if view_id:
417 | if item.MainViewport.Id==view_id: return True
418 | elif item.MainViewport.Name==view: return True
419 | return False
420 |
421 |
422 | def IsViewCurrent(view):
423 | """Verifies that the specified view is the current, or active view
424 | Parameters:
425 | view (str|guid): title or identifier of the view
426 | Returns:
427 | bool: True of False indicating success or failure
428 | Example:
429 | import rhinoscriptsyntax as rs
430 | title = "Perspective"
431 | result = rs.IsViewCurrent(title)
432 | if result:
433 | print("The {} view is current".format(title))
434 | else:
435 | print("The {} view is not current".format(title))
436 | See Also:
437 | CurrentView
438 | """
439 | activeview = scriptcontext.doc.Views.ActiveView
440 | view_id = rhutil.coerceguid(view)
441 | if view_id: return view_id==activeview.MainViewport.Id
442 | return view==activeview.MainViewport.Name
443 |
444 |
445 | def IsViewMaximized(view=None):
446 | """Verifies that the specified view is maximized (enlarged so as to fill
447 | the entire Rhino window)
448 | Parameters:
449 | view: (str|guid): title or identifier of the view. If omitted, the current
450 | view is used
451 | Returns:
452 | bool: True of False
453 | Example:
454 | import rhinoscriptsyntax as rs
455 | title = rs.CurrentView()
456 | result = rs.IsViewMaximized(title)
457 | if result:
458 | print("The {} view is maximized".format(title))
459 | else:
460 | print("The {} view is not maximized".format(title))
461 | See Also:
462 | MaximizeRestoreView
463 | """
464 | view = __viewhelper(view)
465 | return view.Maximized
466 |
467 |
468 | def IsViewPerspective(view):
469 | """Verifies that the specified view's projection is set to perspective
470 | Parameters:
471 | view (str|guid): title or identifier of the view
472 | Returns:
473 | bool: True of False
474 | Example:
475 | import rhinoscriptsyntax as rs
476 | title = rs.CurrentView()
477 | result = rs.IsViewPerspective(title)
478 | if result:
479 | print("The {} view is set to perspective projection".format(title))
480 | else:
481 | print("The {} view is set to parallel projection".format(title))
482 | See Also:
483 | ViewProjection
484 | """
485 | view = __viewhelper(view)
486 | return view.MainViewport.IsPerspectiveProjection
487 |
488 |
489 | def IsViewTitleVisible(view=None):
490 | """Verifies that the specified view's title window is visible
491 | Parameters:
492 | view: (str|guid, optional): The title or identifier of the view. If omitted, the current
493 | active view is used
494 | Returns:
495 | bool: True of False
496 | Example:
497 | import rhinoscriptsyntax as rs
498 | title = rs.CurrentView()
499 | vis = rs.IsViewTitleVisible(title)
500 | if vis:
501 | print("The {} view's title is visible".format(title))
502 | else:
503 | print("The {} view's title is not visible".format(title))
504 | See Also:
505 | ShowViewTitle
506 | """
507 | view = __viewhelper(view)
508 | return view.MainViewport.TitleVisible
509 |
510 |
511 | def IsWallpaper(view):
512 | """Verifies that the specified view contains a wallpaper image
513 | Parameters:
514 | view (str|guid): view to verify
515 | Returns:
516 | bool: True or False
517 | Example:
518 | import rhinoscriptsyntax as rs
519 | view = rs.CurrentView()
520 | filename = rs.OpenFileName()
521 | if filename and not rs.IsWallpaper(view):
522 | rs.Wallpaper(view, filename)
523 | See Also:
524 | Wallpaper
525 | """
526 | view = __viewhelper(view)
527 | return len(view.MainViewport.WallpaperFilename)>0
528 |
529 |
530 | def MaximizeRestoreView(view=None):
531 | """Toggles a view's maximized/restore window state of the specified view
532 | Parameters:
533 | view: (str|guid, optional): the title or identifier of the view. If omitted, the current
534 | active view is used
535 | Returns:
536 | None
537 | Example:
538 | import rhinoscriptsyntax as rs
539 | title = rs.CurrentView()
540 | if rs.IsViewMaximized(title):
541 | rs.MaximizeRestoreView( title )
542 | See Also:
543 | IsViewMaximized
544 | """
545 | view = __viewhelper(view)
546 | view.Maximized = not view.Maximized
547 |
548 |
549 | def NamedCPlane(name):
550 | """Returns the plane geometry of the specified named construction plane
551 | Parameters:
552 | name (str): the name of the construction plane
553 | Returns:
554 | plane: a plane on success
555 | None: on error
556 | Example:
557 | import rhinoscriptsyntax as rs
558 | names = rs.NamedCPlanes()
559 | if names:
560 | for name in names:
561 | plane = rs.NamedCPlane(name)
562 | print("CPlane name:" + name)
563 | print("CPlane origin:" + plane.Origin)
564 | print("CPlane x-axis:" + plane.Xaxis)
565 | print("CPlane y-axis:" + plane.Yaxis)
566 | print("CPlane z-axis:" + plane.Zaxis)
567 | See Also:
568 | AddNamedCPlane
569 | DeleteNamedCPlane
570 | NamedCPlanes
571 | RestoreNamedCPlane
572 | """
573 | index = scriptcontext.doc.NamedConstructionPlanes.Find(name)
574 | if index<0: return scriptcontext.errorhandler()
575 | return scriptcontext.doc.NamedConstructionPlanes[index].Plane
576 |
577 |
578 | def NamedCPlanes():
579 | """Returns the names of all named construction planes in the document
580 | Returns:
581 | list(str, ...): the names of all named construction planes in the document
582 | Example:
583 | import rhinoscriptsyntax as rs
584 | cplanes = rs.NamedCPlanes()
585 | if cplanes:
586 | for cplane in cplanes: print(cplane)
587 | See Also:
588 | AddNamedCPlane
589 | DeleteNamedCPlane
590 | NamedCPlane
591 | RestoreNamedCPlane
592 | """
593 | count = scriptcontext.doc.NamedConstructionPlanes.Count
594 | rc = [scriptcontext.doc.NamedConstructionPlanes[i].Name for i in range(count)]
595 | return rc
596 |
597 |
598 | def NamedViews():
599 | """Returns the names of all named views in the document
600 | Returns:
601 | list(str, ...): the names of all named views in the document
602 | Example:
603 | import rhinoscriptsyntax as rs
604 | views = rs.NamedViews()
605 | if views:
606 | for view in views: print(view)
607 | See Also:
608 | AddNamedView
609 | DeleteNamedView
610 | RestoreNamedView
611 | """
612 | count = scriptcontext.doc.NamedViews.Count
613 | return [scriptcontext.doc.NamedViews[i].Name for i in range(count)]
614 |
615 |
616 | def RenameView(old_title, new_title):
617 | """Changes the title of the specified view
618 | Parameters:
619 | old_title (str|guid): the title or identifier of the view to rename
620 | new_title (str): the new title of the view
621 | Returns:
622 | str: the view's previous title if successful
623 | None: on error
624 | Example:
625 | import rhinoscriptsyntax as rs
626 | oldtitle = rs.CurrentView()
627 | rs.renameview( oldtitle, "Current" )
628 | See Also:
629 | ViewNames
630 | """
631 | if not old_title or not new_title: return scriptcontext.errorhandler()
632 | old_id = rhutil.coerceguid(old_title)
633 | foundview = None
634 | allviews = scriptcontext.doc.Views.GetViewList(True, True)
635 | for view in allviews:
636 | if old_id:
637 | if view.MainViewport.Id==old_id:
638 | foundview = view
639 | break
640 | elif view.MainViewport.Name==old_title:
641 | foundview = view
642 | break
643 | if foundview is None: return scriptcontext.errorhandler()
644 | old_title = foundview.MainViewport.Name
645 | foundview.MainViewport.Name = new_title
646 | return old_title
647 |
648 |
649 | def RestoreNamedCPlane(cplane_name, view=None):
650 | """Restores a named construction plane to the specified view.
651 | Parameters:
652 | cplane_name (str): name of the construction plane to restore
653 | view: (str|guid, optional): the title or identifier of the view. If omitted, the current
654 | active view is used
655 | Returns:
656 | str: name of the restored named construction plane if successful
657 | None: on error
658 | Example:
659 | import rhinoscriptsyntax as rs
660 | cplanes = rs.NamedCplanes()
661 | if cplanes: rs.RestoreNamedCPlane( cplanes[0] )
662 | See Also:
663 | AddNamedCPlane
664 | DeleteNamedCPlane
665 | NamedCPlane
666 | NamedCPlanes
667 | """
668 | view = __viewhelper(view)
669 | index = scriptcontext.doc.NamedConstructionPlanes.Find(cplane_name)
670 | if index<0: return scriptcontext.errorhandler()
671 | cplane = scriptcontext.doc.NamedConstructionPlanes[index]
672 | view.MainViewport.PushConstructionPlane(cplane)
673 | view.Redraw()
674 | return cplane_name
675 |
676 |
677 | def RestoreNamedView(named_view, view=None, restore_bitmap=False):
678 | """Restores a named view to the specified view
679 | Parameters:
680 | named_view (str): name of the named view to restore
681 | view (str|guid, optional): title or id of the view to restore the named view.
682 | If omitted, the current active view is used
683 | restore_bitmap: (bool, optional): restore the named view's background bitmap
684 | Returns:
685 | str: name of the restored view if successful
686 | None: on error
687 | Example:
688 | import rhinoscriptsyntax as rs
689 | views = rs.NamedViews()
690 | if views: rs.RestoreNamedView(views[0])
691 | See Also:
692 | AddNamedView
693 | DeleteNamedView
694 | NamedViews
695 | """
696 | view = __viewhelper(view)
697 | index = scriptcontext.doc.NamedViews.FindByName(named_view)
698 | if index<0: return scriptcontext.errorhandler()
699 | viewinfo = scriptcontext.doc.NamedViews[index]
700 | if view.MainViewport.PushViewInfo(viewinfo, restore_bitmap):
701 | view.Redraw()
702 | return view.MainViewport.Name
703 | return scriptcontext.errorhandler()
704 |
705 |
706 | def RotateCamera(view=None, direction=0, angle=None):
707 | """Rotates a perspective-projection view's camera. See the RotateCamera
708 | command in the Rhino help file for more details
709 | Parameters:
710 | view (str|guid, optional): title or id of the view. If omitted, current active view is used
711 | direction(number, optional): the direction to rotate the camera where
712 | 0=right
713 | 1=left
714 | 2=down
715 | 3=up
716 | angle: (number, optional): the angle to rotate. If omitted, the angle of rotation
717 | is specified by the "Increment in divisions of a circle" parameter
718 | specified in Options command's View tab
719 | Returns:
720 | bool: True or False indicating success or failure
721 | Example:
722 | import rhinoscriptsyntax as rs
723 | rs.RotateCamera( angle=15 )
724 | See Also:
725 | RotateView
726 | TiltView
727 | """
728 | view = __viewhelper(view)
729 | viewport = view.ActiveViewport
730 | if angle is None:
731 | angle = 2.0*math.pi/Rhino.ApplicationSettings.ViewSettings.RotateCircleIncrement
732 | else:
733 | angle = Rhino.RhinoMath.ToRadians( abs(angle) )
734 | target_distance = (viewport.CameraLocation-viewport.CameraTarget)*viewport.CameraZ
735 | axis = viewport.CameraY
736 | if direction==0 or direction==2: angle=-angle
737 | if direction==0 or direction==1:
738 | if Rhino.ApplicationSettings.ViewSettings.RotateToView:
739 | axis = viewport.CameraY
740 | else:
741 | axis = Rhino.Geometry.Vector3d.ZAxis
742 | elif direction==2 or direction==3:
743 | axis = viewport.CameraX
744 | else:
745 | return False
746 | if Rhino.ApplicationSettings.ViewSettings.RotateReverseKeyboard: angle=-angle
747 | rot = Rhino.Geometry.Transform.Rotation(angle, axis, Rhino.Geometry.Point3d.Origin)
748 | camUp = rot * viewport.CameraY
749 | camDir = -(rot * viewport.CameraZ)
750 | target = viewport.CameraLocation + target_distance*camDir
751 | viewport.SetCameraLocations(target, viewport.CameraLocation)
752 | viewport.CameraUp = camUp
753 | view.Redraw()
754 | return True
755 |
756 |
757 | def RotateView(view=None, direction=0, angle=None):
758 | """Rotates a view. See RotateView command in Rhino help for more information
759 | Parameters:
760 | view (str|guid, optional): title or id of the view. If omitted, the current active view is used
761 | direction (number, optional): the direction to rotate the view where
762 | 0=right
763 | 1=left
764 | 2=down
765 | 3=up
766 | angle (number): angle to rotate. If omitted, the angle of rotation is specified
767 | by the "Increment in divisions of a circle" parameter specified in
768 | Options command's View tab
769 | Returns:
770 | bool: True or False indicating success or failure
771 | Example:
772 | import rhinoscriptsyntax as rs
773 | rs.RotateView( angle=90.0 )
774 | See Also:
775 | RotateCamera
776 | TiltView
777 | """
778 | view = __viewhelper(view)
779 | viewport = view.ActiveViewport
780 | if angle is None:
781 | angle = 2.0*math.pi/Rhino.ApplicationSettings.ViewSettings.RotateCircleIncrement
782 | else:
783 | angle = Rhino.RhinoMath.ToRadians( abs(angle) )
784 | if Rhino.ApplicationSettings.ViewSettings.RotateReverseKeyboard: angle = -angle
785 | if direction==0: viewport.KeyboardRotate(True, angle)
786 | elif direction==1: viewport.KeyboardRotate(True, -angle)
787 | elif direction==2: viewport.KeyboardRotate(False, -angle)
788 | elif direction==3: viewport.KeyboardRotate(False, angle)
789 | else: return False
790 | view.Redraw()
791 | return True
792 |
793 |
794 | def ShowGrid(view=None, show=None):
795 | """Shows or hides a view's construction plane grid
796 | Parameters:
797 | view (str|guid, optional): title or id of the view. If omitted, the current active view is used
798 | show (bool, optional): The grid state to set. If omitted, the current grid display state is returned
799 | Returns:
800 | bool: If show is not specified, then the grid display state if successful
801 | bool: If show is specified, then the previous grid display state if successful
802 | Example:
803 | import rhinoscriptsyntax as rs
804 | view = rs.CurrentView()
805 | if rs.ShowGrid(view)==False:
806 | rs.ShowGrid( view, True )
807 | See Also:
808 | ShowGridAxes
809 | ShowWorldAxes
810 | """
811 | view = __viewhelper(view)
812 | viewport = view.ActiveViewport
813 | rc = viewport.ConstructionGridVisible
814 | if show is not None and rc!=show:
815 | viewport.ConstructionGridVisible = show
816 | view.Redraw()
817 | return rc
818 |
819 |
820 | def ShowGridAxes(view=None, show=None):
821 | """Shows or hides a view's construction plane grid axes.
822 | Parameters:
823 | view (str|guid, optional): title or id of the view. If omitted, the current active view is used
824 | show (bool, optional): The state to set. If omitted, the current grid axes display state is returned
825 | Returns:
826 | bool: If show is not specified, then the grid axes display state
827 | bool: If show is specified, then the previous grid axes display state
828 | Example:
829 | import rhinoscriptsyntax as rs
830 | view = rs.CurrentView()
831 | if rs.ShowGridAxes(view)==False:
832 | rs.ShowGridAxes( view, True )
833 | See Also:
834 | ShowGrid
835 | ShowWorldAxes
836 | """
837 | view = __viewhelper(view)
838 | viewport = view.ActiveViewport
839 | rc = viewport.ConstructionAxesVisible
840 | if show is not None and rc!=show:
841 | viewport.ConstructionAxesVisible = show
842 | view.Redraw()
843 | return rc
844 |
845 |
846 | def ShowViewTitle(view=None, show=True):
847 | """Shows or hides the title window of a view
848 | Parameters:
849 | view (str|guid, optional): title or id of the view. If omitted, the current active view is used
850 | show (bool, optional): The state to set.
851 | Returns:
852 | None
853 | Example:
854 | import rhinoscriptsyntax as rs
855 | view = rs.CurrentView()
856 | if rs.IsViewTitleVisible(view)==False:
857 | rs.ShowViewTitle( view, True )
858 | See Also:
859 | IsViewTitleVisible
860 | """
861 | view = __viewhelper(view)
862 | if view is None: return scriptcontext.errorhandler()
863 | view.TitleVisible = show
864 |
865 |
866 | def ShowWorldAxes(view=None, show=None):
867 | """Shows or hides a view's world axis icon
868 | Parameters:
869 | view (str|guid, optional): title or id of the view. If omitted, the current active view is used
870 | show: (bool, optional): The state to set.
871 | Returns:
872 | bool: If show is not specified, then the world axes display state
873 | bool: If show is specified, then the previous world axes display state
874 | Example:
875 | import rhinoscriptsyntax as rs
876 | view = rs.CurrentView()
877 | if rs.ShowWorldAxes(view)==False:
878 | rs.ShowWorldAxes( view, True )
879 | See Also:
880 | ShowGrid
881 | ShowGridAxes
882 | """
883 | view = __viewhelper(view)
884 | viewport = view.ActiveViewport
885 | rc = viewport.WorldAxesVisible
886 | if show is not None and rc!=show:
887 | viewport.WorldAxesVisible = show
888 | view.Redraw()
889 | return rc
890 |
891 |
892 | def TiltView(view=None, direction=0, angle=None):
893 | """Tilts a view by rotating the camera up vector. See the TiltView command in
894 | the Rhino help file for more details.
895 | Parameters:
896 | view (str|guid, optional): title or id of the view. If omitted, the current active view is used
897 | direction (number, optional): the direction to rotate the view where
898 | 0=right
899 | 1=left
900 | angle (number, optional): the angle to rotate. If omitted, the angle of rotation is
901 | specified by the "Increment in divisions of a circle" parameter specified
902 | in Options command's View tab
903 | Returns:
904 | bool: True or False indicating success or failure
905 | Example:
906 | import rhinoscriptsyntax as rs
907 | rs.TiltView( angle=15 )
908 | See Also:
909 | RotateCamera
910 | """
911 | view = __viewhelper(view)
912 | viewport = view.ActiveViewport
913 | if angle is None:
914 | angle = 2.0*math.pi/Rhino.ApplicationSettings.ViewSettings.RotateCircleIncrement
915 | else:
916 | angle = Rhino.RhinoMath.ToRadians( abs(angle) )
917 |
918 | if Rhino.ApplicationSettings.ViewSettings.RotateReverseKeyboard: angle = -angle
919 | axis = viewport.CameraLocation - viewport.CameraTarget
920 | if direction==0: viewport.Rotate(angle, axis, viewport.CameraLocation)
921 | elif direction==1: viewport.Rotate(-angle, axis, viewport.CameraLocation)
922 | else: return False
923 | view.Redraw()
924 | return True
925 |
926 |
927 | def ViewCamera(view=None, camera_location=None):
928 | """Returns or sets the camera location of the specified view
929 | Parameters:
930 | view (str|guid, optional): title or id of the view. If omitted, the current active view is used
931 | camera_location (point, optional): a 3D point identifying the new camera location.
932 | If omitted, the current camera location is returned
933 | Returns:
934 | point: If camera_location is not specified, the current camera location
935 | point: If camera_location is specified, the previous camera location
936 | None: on error
937 | Example:
938 | import rhinoscriptsyntax as rs
939 | view = rs.CurrentView()
940 | camera = rs.GetPoint("Select new camera location")
941 | if camera: rs.ViewCamera(view,camera)
942 | See Also:
943 | ViewCameraTarget
944 | ViewTarget
945 | """
946 | view = __viewhelper(view)
947 | rc = view.ActiveViewport.CameraLocation
948 | if camera_location is None: return rc
949 | camera_location = rhutil.coerce3dpoint(camera_location)
950 | if camera_location is None: return scriptcontext.errorhandler()
951 | view.ActiveViewport.SetCameraLocation(camera_location, True)
952 | view.Redraw()
953 | return rc
954 |
955 |
956 | def ViewCameraLens(view=None, length=None):
957 | """Returns or sets the 35mm camera lens length of the specified perspective
958 | projection view.
959 | Parameters:
960 | view (str|guid, optional): title or id of the view. If omitted, the current active view is used
961 | length (number, optional): the new 35mm camera lens length. If omitted, the previous
962 | 35mm camera lens length is returned
963 | Returns:
964 | number: If lens length is not specified, the current lens length
965 | number: If lens length is specified, the previous lens length
966 | Example:
967 | import rhinoscriptsyntax as rs
968 | view = rs.CurrentView()
969 | if rs.IsViewPerspective(view):
970 | length = rs.ViewCameraLens(view, 100)
971 | See Also:
972 | ViewCameraTarget
973 | ViewCPlane
974 | ViewDisplayModes
975 | ViewProjection
976 | ViewSize
977 | """
978 | view = __viewhelper(view)
979 | rc = view.ActiveViewport.Camera35mmLensLength
980 | if not length: return rc
981 | view.ActiveViewport.Camera35mmLensLength = length
982 | view.Redraw()
983 | return rc
984 |
985 |
986 | def ViewCameraPlane(view=None):
987 | """Returns the orientation of a view's camera.
988 | Parameters:
989 | view (str|guid, optional): title or id of the view. If omitted, the current active view is used
990 | Returns:
991 | plane: the view's camera plane if successful
992 | None: on error
993 | Example:
994 | import rhinoscriptsyntax as rs
995 | view = rs.CurrentView()
996 | target = rs.ViewTarget(view)
997 | camplane = rs.ViewCameraPlane(view)
998 | plane = rs.MovePlane(camplane, target)
999 | rs.ViewCPlane( view, plane )
1000 | See Also:
1001 | ViewCamera
1002 | ViewTarget
1003 | """
1004 | view = __viewhelper(view)
1005 | rc, frame = view.ActiveViewport.GetCameraFrame()
1006 | if not rc: return scriptcontext.errorhandler()
1007 | return frame
1008 |
1009 |
1010 | def ViewCameraTarget(view=None, camera=None, target=None):
1011 | """Returns or sets the camera and target positions of the specified view
1012 | Parameters:
1013 | view (str|guid, optional): title or id of the view. If omitted, current active view is used
1014 | camera (point): 3d point identifying the new camera location. If camera and
1015 | target are not specified, current camera and target locations are returned
1016 | target (point): 3d point identifying the new target location. If camera and
1017 | target are not specified, current camera and target locations are returned
1018 | Returns:
1019 | list(point, point): if both camera and target are not specified, then the 3d points containing
1020 | the current camera and target locations is returned
1021 | point: if either camera or target are specified, then the 3d points containing the
1022 | previous camera and target locations is returned
1023 | Example:
1024 | import rhinoscriptsyntax as rs
1025 | view = rs.CurrentView()
1026 | camera = rs.GetPoint("Select new camera location")
1027 | target = rs.GetPoint("Select new target location")
1028 | if camera and target:
1029 | rs.ViewCameraTarget( view, camera, target )
1030 | See Also:
1031 | ViewCamera
1032 | ViewTarget
1033 | """
1034 | view = __viewhelper(view)
1035 | rc = view.ActiveViewport.CameraLocation, view.ActiveViewport.CameraTarget
1036 | if not camera and not target: return rc
1037 | if camera: camera = rhutil.coerce3dpoint(camera, True)
1038 | if target: target = rhutil.coerce3dpoint(target, True)
1039 | if camera and target: view.ActiveViewport.SetCameraLocations(target, camera)
1040 | elif camera is None: view.ActiveViewport.SetCameraTarget(target, True)
1041 | else: view.ActiveViewport.SetCameraLocation(camera, True)
1042 | view.Redraw()
1043 | return rc
1044 |
1045 |
1046 | def ViewCameraUp(view=None, up_vector=None):
1047 | """Returns or sets the camera up direction of a specified
1048 | Parameters:
1049 | view (str|guid, optional): title or id of the view. If omitted, the current active view is used
1050 | up_vector (vector): 3D vector identifying the new camera up direction
1051 | Returns:
1052 | vector: if up_vector is not specified, then the current camera up direction
1053 | vector: if up_vector is specified, then the previous camera up direction
1054 | Example:
1055 | import rhinoscriptsyntax as rs
1056 | view = rs.CurrentView()
1057 | upVector = rs.ViewCameraUp(view)
1058 | print(up_vector)
1059 | See Also:
1060 | ViewCamera
1061 | ViewTarget
1062 | """
1063 | view = __viewhelper(view)
1064 | rc = view.ActiveViewport.CameraUp
1065 | if up_vector:
1066 | view.ActiveViewport.CameraUp = rhutil.coerce3dvector(up_vector, True)
1067 | view.Redraw()
1068 | return rc
1069 |
1070 |
1071 | def ViewCPlane(view=None, plane=None):
1072 | """Return or set a view's construction plane
1073 | Parameters:
1074 | view (str|guid, optional): title or id of the view. If omitted, current active view is used.
1075 | plane (plane): the new construction plane if setting
1076 | Returns:
1077 | plane: If a construction plane is not specified, the current construction plane
1078 | plane: If a construction plane is specified, the previous construction plane
1079 | Example:
1080 | import rhinoscriptsyntax as rs
1081 | origin = rs.GetPoint("CPlane origin")
1082 | if origin:
1083 | plane = rs.ViewCPlane()
1084 | plane = rs.MovePlane(plane,origin)
1085 | rs.ViewCPlane(None, plane)
1086 | See Also:
1087 | ViewCameraLens
1088 | ViewCameraTarget
1089 | ViewDisplayModes
1090 | ViewProjection
1091 | ViewSize
1092 | """
1093 | view = __viewhelper(view)
1094 | cplane = view.ActiveViewport.ConstructionPlane()
1095 | if plane:
1096 | plane = rhutil.coerceplane(plane, True)
1097 | view.ActiveViewport.SetConstructionPlane(plane)
1098 | view.Redraw()
1099 | return cplane
1100 |
1101 |
1102 | def ViewDisplayMode(view=None, mode=None, return_name=True):
1103 | """Return or set a view display mode
1104 | Parameters:
1105 | view (str|guid, optional): Title or id of a view. If omitted, active view is used
1106 | mode (str|guid, optional): Name or id of a display mode
1107 | return_name (bool, optional): If true, return display mode name. If False, display mode id
1108 | Returns:
1109 | str: If mode is specified, the previous mode
1110 | str: If mode is not specified, the current mode
1111 | Example:
1112 | import rhinoscriptsyntax as rs
1113 | views = rs.ViewNames()
1114 | for view in views:
1115 | rs.ViewDisplayMode(view, 'Ghosted')
1116 | See Also:
1117 | CurrentView
1118 | ViewNames
1119 | """
1120 | view = __viewhelper(view)
1121 | current = view.ActiveViewport.DisplayMode
1122 | if return_name: rc = current.EnglishName
1123 | else: rc = current.Id
1124 | if mode:
1125 | mode_id = rhutil.coerceguid(mode)
1126 | if mode_id:
1127 | desc = Rhino.Display.DisplayModeDescription.GetDisplayMode(mode_id)
1128 | else:
1129 | desc = Rhino.Display.DisplayModeDescription.FindByName(mode)
1130 | if desc: view.ActiveViewport.DisplayMode = desc
1131 | scriptcontext.doc.Views.Redraw()
1132 | return rc
1133 |
1134 |
1135 | def ViewDisplayModeId(name):
1136 | """Return id of a display mode given it's name
1137 | Parameters:
1138 | name (str): name of the display mode
1139 | Returns:
1140 | guid: The id of the display mode if successful, otherwise None
1141 | Example:
1142 | import rhinoscriptsyntax as rs
1143 | modes = rs.ViewDisplayModes(True)
1144 | for mode in modes: print("{} = {}".format(mode, rs.ViewDisplayModeId(mode)))
1145 | See Also:
1146 | ViewDisplayMode
1147 | ViewDisplayModes
1148 | """
1149 | desc = Rhino.Display.DisplayModeDescription.FindByName(name)
1150 | if desc: return desc.Id
1151 |
1152 |
1153 | def ViewDisplayModeName(mode_id):
1154 | """Return name of a display mode given it's id
1155 | Parameters:
1156 | mode_id (guid): The identifier of the display mode obtained from the ViewDisplayModes method.
1157 | Returns:
1158 | str: The name of the display mode if successful, otherwise None
1159 | Example:
1160 | import rhinoscriptsyntax as rs
1161 | modes = rs.ViewDisplayModes(False)
1162 | for mode in modes: print("{} = {}".format(mode, rs.ViewDisplayModeName(mode)))
1163 | See Also:
1164 | ViewDisplayMode
1165 | ViewDisplayModes
1166 | """
1167 | mode_id = rhutil.coerceguid(mode_id, True)
1168 | desc = Rhino.Display.DisplayModeDescription.GetDisplayMode(mode_id)
1169 | if desc: return desc.EnglishName
1170 |
1171 |
1172 | def ViewDisplayModes(return_names=True):
1173 | """Return list of display modes
1174 | Parameters:
1175 | return_name (bool, otpional): If True, return mode names. If False, return ids
1176 | Returns:
1177 | list(str|guid, ...): strings identifying the display mode names or identifiers if successful
1178 | Example:
1179 | import rhinoscriptsyntax as rs
1180 | modes = rs.ViewDisplayModes(False)
1181 | for mode in modes: print("{} = {}".format(mode, rs.ViewDisplayModeName(mode)))
1182 | See Also:
1183 | ViewDisplayMode
1184 | ViewDisplayModeName
1185 | """
1186 | modes = Rhino.Display.DisplayModeDescription.GetDisplayModes()
1187 | if return_names:
1188 | return [mode.EnglishName for mode in modes]
1189 | return [mode.Id for mode in modes]
1190 |
1191 |
1192 | def ViewNames(return_names=True, view_type=0):
1193 | """Return the names, titles, or identifiers of all views in the document
1194 | Parameters:
1195 | return_names (bool, optional): if True then the names of the views are returned.
1196 | If False, then the identifiers of the views are returned
1197 | view_type: (number, optional): the type of view to return
1198 | 0 = standard model views
1199 | 1 = page layout views
1200 | 2 = both standard and page layout views
1201 | Returns:
1202 | list(str|guid, ...): of the view names or identifiers on success
1203 | None: on error
1204 | Example:
1205 | import rhinoscriptsyntax as rs
1206 | # Print view names
1207 | views = rs.ViewNames()
1208 | if views:
1209 | for view in views: print(view)
1210 | # Print view identifiers
1211 | view_ids = rs.ViewNames(False)
1212 | if view_ids:
1213 | for id in view_ids:
1214 | print("{} = {}".format(id, rs.ViewTitle(id)))
1215 | See Also:
1216 | IsView
1217 | ViewTitle
1218 | """
1219 | views = scriptcontext.doc.Views.GetViewList(view_type!=1, view_type>0)
1220 | if views is None: return scriptcontext.errorhandler()
1221 | if return_names: return [view.MainViewport.Name for view in views]
1222 | return [view.MainViewport.Id for view in views]
1223 |
1224 |
1225 | def ViewNearCorners(view=None):
1226 | """Return 3d corners of a view's near clipping plane rectangle. Useful
1227 | in determining the "real world" size of a parallel-projected view
1228 | Parameters:
1229 | view (str|guid, optional): title or id of the view. If omitted, current active view is used
1230 | Returns:
1231 | list(point, point, point, point): Four Point3d that define the corners of the rectangle (counter-clockwise order)
1232 | Example:
1233 | import rhinoscriptsyntax as rs
1234 | rect = rs.ViewNearCorners()
1235 | if rect:
1236 | for i in range(4): rs.AddTextDot( i, rect[i] )
1237 | See Also:
1238 | CurrentView
1239 | """
1240 | view = __viewhelper(view)
1241 | rc = view.ActiveViewport.GetNearRect()
1242 | return rc[0], rc[1], rc[3], rc[2]
1243 |
1244 |
1245 | def ViewProjection(view=None, mode=None):
1246 | """Return or set a view's projection mode.
1247 | Parameters:
1248 | view (str|guid, optional): title or id of the view. If omitted, current active view is used
1249 | mode (number, optional): the projection mode
1250 | 1 = parallel
1251 | 2 = perspective
1252 | 3 = two point perspective
1253 | Returns:
1254 | number: if mode is not specified, the current projection mode for the specified view
1255 | number: if mode is specified, the previous projection mode for the specified view
1256 | Example:
1257 | import rhinoscriptsyntax as rs
1258 | views = rs.ViewNames()
1259 | if views:
1260 | for view in views: rs.ViewProjection(view,1)
1261 | See Also:
1262 | IsViewPerspective
1263 | """
1264 | view = __viewhelper(view)
1265 | viewport = view.ActiveViewport
1266 | rc = 2
1267 | if viewport.IsParallelProjection: rc = 1
1268 | elif viewport.IsTwoPointPerspectiveProjection: rc = 3
1269 | if mode is None or mode==rc: return rc
1270 | if mode==1: viewport.ChangeToParallelProjection(True)
1271 | elif mode==2: viewport.ChangeToPerspectiveProjection(True, 50)
1272 | elif mode==3: viewport.ChangeToTwoPointPerspectiveProjection(50)
1273 | else: return
1274 | view.Redraw()
1275 | return rc
1276 |
1277 |
1278 | def ViewRadius(view=None, radius=None, mode=False):
1279 | """Returns or sets the radius of a parallel-projected view. Useful
1280 | when you need an absolute zoom factor for a parallel-projected view
1281 | Parameters:
1282 | view (str|guid, optional): title or id of the view. If omitted, current active view is used
1283 | radius (number): the view radius
1284 | mode (bool, optional): perform a "dolly" magnification by moving the camera
1285 | towards/away from the target so that the amount of the screen
1286 | subtended by an object changes. true = perform a "zoom"
1287 | magnification by adjusting the "lens" angle
1288 | Returns:
1289 | number: if radius is not specified, the current view radius for the specified view
1290 | number: if radius is specified, the previous view radius for the specified view
1291 | Example:
1292 | import rhinoscriptsyntax as rs
1293 | rhParallelView = 1
1294 | views = rs.ViewNames()
1295 | if views:
1296 | for view in views:
1297 | if rs.ViewProjection(view)==rhParallelView:
1298 | rs.ViewRadius(view, 10.0)
1299 | See Also:
1300 | IsViewPerspective
1301 | ViewProjection
1302 | """
1303 | view = __viewhelper(view)
1304 | viewport = view.ActiveViewport
1305 | if not viewport.IsParallelProjection: return scriptcontext.errorhandler()
1306 | fr = viewport.GetFrustum()
1307 | frus_right = fr[2]
1308 | frus_top = fr[4]
1309 | old_radius = min(frus_top, frus_right)
1310 | if radius is None: return old_radius
1311 | magnification_factor = radius / old_radius
1312 | d = 1.0 / magnification_factor
1313 | viewport.Magnify(d, mode)
1314 | view.Redraw()
1315 | return old_radius
1316 |
1317 |
1318 | def ViewSize(view=None):
1319 | """Returns the width and height in pixels of the specified view
1320 | Parameters:
1321 | view (str|guid): title or id of the view. If omitted, current active view is used
1322 | Returns:
1323 | tuple(number, number): of two numbers identifying width and height
1324 | Example:
1325 | import rhinoscriptsyntax as rs
1326 | size = rs.ViewSize()
1327 | if size:
1328 | print("Width: {} pixels".format(size[0]))
1329 | print("Height: {} pixels".format(size[1]))
1330 | See Also:
1331 | ViewCameraLens
1332 | ViewCameraTarget
1333 | ViewCPlane
1334 | ViewDisplayModes
1335 | ViewProjection
1336 | """
1337 | view = __viewhelper(view)
1338 | cr = view.ClientRectangle
1339 | return cr.Width, cr.Height
1340 |
1341 |
1342 | def ViewSpeedTest(view=None, frames=100, freeze=True, direction=0, angle_degrees=5):
1343 | """Test's Rhino's display performance
1344 | Parameters:
1345 | view (str|guid, optional): The title or identifier of the view. If omitted, the current active view is used
1346 | frames (number, optional): The number of frames, or times to regenerate the view. If omitted, the view will be regenerated 100 times.
1347 | freeze (bool, optional): If True (Default), then Rhino's display list will not be updated with every frame redraw. If False, then Rhino's display list will be updated with every frame redraw.
1348 | direction (number, optional): The direction to rotate the view. The default direction is Right (0). Modes:
1349 | 0 = Right
1350 | 1 = Left
1351 | 2 = Down
1352 | 3 = Up.
1353 | angle_degrees (number, optional): The angle to rotate. If omitted, the rotation angle of 5.0 degrees will be used.
1354 | Returns:
1355 | number: The number of seconds it took to regenerate the view frames number of times, if successful
1356 | None: if not successful
1357 | Example:
1358 | import rhinoscriptsyntax as rs
1359 | view = "Perspective"
1360 | seconds = rs.ViewSpeedTest(view, 100)
1361 | if seconds:
1362 | print("Time to regen viewport 100 times = {} secords".format(seconds))
1363 | See Also:
1364 |
1365 | """
1366 | view = __viewhelper(view)
1367 | angle_radians = math.radians(angle_degrees)
1368 | return view.SpeedTest(frames, freeze, direction, angle_radians)
1369 |
1370 |
1371 | def ViewTarget(view=None, target=None):
1372 | """Returns or sets the target location of the specified view
1373 | Parameters:
1374 | view (str|guid, optional): title or id of the view. If omitted, current active view is used
1375 | target (point, optional): 3d point identifying the new target location. If omitted,
1376 | the current target location is returned
1377 | Returns:
1378 | point: is target is not specified, then the current target location
1379 | point: is target is specified, then the previous target location
1380 | None: on error
1381 | Example:
1382 | import rhinoscriptsyntax as rs
1383 | view = rs.CurrentView()
1384 | target = rs.GetPoint("Select new target location")
1385 | if target: rs.ViewTarget( view, target )
1386 | See Also:
1387 | ViewCamera
1388 | ViewCameraTarget
1389 | """
1390 | view = __viewhelper(view)
1391 | viewport = view.ActiveViewport
1392 | old_target = viewport.CameraTarget
1393 | if target is None: return old_target
1394 | target = rhutil.coerce3dpoint(target)
1395 | if target is None: return scriptcontext.errorhandler()
1396 | viewport.SetCameraTarget(target, True)
1397 | view.Redraw()
1398 | return old_target
1399 |
1400 |
1401 | def ViewTitle(view_id):
1402 | """Returns the name, or title, of a given view's identifier
1403 | Parameters:
1404 | view_id (str|guid): The identifier of the view
1405 | Returns:
1406 | str: name or title of the view on success
1407 | None: on error
1408 | Example:
1409 | import rhinoscriptsyntax as rs
1410 | view_ids = rs.ViewNames(False)
1411 | for id in view_ids:
1412 | print(id + " = " + rs.ViewTitle(id))
1413 | See Also:
1414 | CurrentView
1415 | ViewNames
1416 | """
1417 | view_id = rhutil.coerceguid(view_id)
1418 | if view_id is None: return scriptcontext.errorhandler()
1419 | view = scriptcontext.doc.Views.Find(view_id)
1420 | if view is None: return scriptcontext.errorhandler()
1421 | return view.MainViewport.Name
1422 |
1423 |
1424 | def Wallpaper(view=None, filename=None):
1425 | """Returns or sets the wallpaper bitmap of the specified view. To remove a
1426 | wallpaper bitmap, pass an empty string ""
1427 | Parameters:
1428 | view (str|guid, optional): The identifier of the view. If omitted, the
1429 | active view is used
1430 | filename (str): Name of the bitmap file to set as wallpaper
1431 | Returns:
1432 | str: If filename is not specified, the current wallpaper bitmap filename
1433 | str: If filename is specified, the previous wallpaper bitmap filename
1434 | Example:
1435 | import rhinoscriptsyntax as rs
1436 | view = rs.CurrentView()
1437 | filename = rs.OpenFileName()
1438 | if filename and not rs.IsWallpaper(view):
1439 | rs.Wallpaper(view, filename)
1440 | See Also:
1441 | IsWallpaper
1442 | WallpaperGrayScale
1443 | WallpaperHidden
1444 | """
1445 | view = __viewhelper(view)
1446 | rc = view.ActiveViewport.WallpaperFilename
1447 | if filename is not None and filename!=rc:
1448 | view.ActiveViewport.SetWallpaper(filename, False)
1449 | view.Redraw()
1450 | return rc
1451 |
1452 |
1453 | def WallpaperGrayScale(view=None, grayscale=None):
1454 | """Returns or sets the grayscale display option of the wallpaper bitmap in a
1455 | specified view
1456 | Parameters:
1457 | view (str|guid, optional): The identifier of the view. If omitted, the
1458 | active view is used
1459 | grayscale (bool, optional): Display the wallpaper in gray(True) or color (False)
1460 | Returns:
1461 | bool: If grayscale is not specified, the current grayscale display option
1462 | bool: If grayscale is specified, the previous grayscale display option
1463 | Example:
1464 | import rhinoscriptsyntax as rs
1465 | view = rs.CurrentView()
1466 | if rs.WallpaperGrayScale(view)==False: rs.WallpaperGrayScale(view, True)
1467 | See Also:
1468 | Wallpaper
1469 | WallpaperHidden
1470 | """
1471 | view = __viewhelper(view)
1472 | rc = view.ActiveViewport.WallpaperGrayscale
1473 | if grayscale is not None and grayscale!=rc:
1474 | filename = view.ActiveViewport.WallpaperFilename
1475 | view.ActiveViewport.SetWallpaper(filename, grayscale)
1476 | view.Redraw()
1477 | return rc
1478 |
1479 |
1480 | def WallpaperHidden(view=None, hidden=None):
1481 | """Returns or sets the visibility of the wallpaper bitmap in a specified view
1482 | Parameters:
1483 | view (str|guid, optional): The identifier of the view. If omitted, the
1484 | active view is used
1485 | hidden (bool, optional): Show or hide the wallpaper
1486 | Returns:
1487 | bool: If hidden is not specified, the current hidden state
1488 | bool: If hidden is specified, the previous hidden state
1489 | Example:
1490 | import rhinoscriptsyntax as rs
1491 | view = rs.CurrentView()
1492 | if rs.WallpaperHidden(view) == False: rs.WallpaperHidden(view, True)
1493 | See Also:
1494 | Wallpaper
1495 | WallpaperGrayScale
1496 | """
1497 | view = __viewhelper(view)
1498 | rc = not view.ActiveViewport.WallpaperVisible
1499 | if hidden is not None and hidden!=rc:
1500 | filename = view.ActiveViewport.WallpaperFilename
1501 | gray = view.ActiveViewport.WallpaperGrayscale
1502 | view.ActiveViewport.SetWallpaper(filename, gray, not hidden)
1503 | view.Redraw()
1504 | return rc
1505 |
1506 |
1507 | def ZoomBoundingBox(bounding_box, view=None, all=False):
1508 | """Zooms to the extents of a specified bounding box in the specified view
1509 | Parameters:
1510 | bounding_box ([point, point, point ,point, point, point, point, point]): eight points that define the corners
1511 | of a bounding box or a BoundingBox class instance
1512 | view (str|guid, optional): title or id of the view. If omitted, current active view is used
1513 | all (bool, optional): zoom extents in all views
1514 | Returns:
1515 | None
1516 | Example:
1517 | import rhinoscriptsyntax as rs
1518 | obj = rs.GetObject()
1519 | if obj:
1520 | bbox = rs.BoundingBox(obj)
1521 | rs.ZoomBoundingBox( bbox )
1522 | See Also:
1523 | ZoomExtents
1524 | ZoomSelected
1525 | """
1526 | bbox = rhutil.coerceboundingbox(bounding_box)
1527 | if bbox:
1528 | if all:
1529 | views = scriptcontext.doc.Views.GetViewList(True, True)
1530 | for view in views: view.ActiveViewport.ZoomBoundingBox(bbox)
1531 | else:
1532 | view = __viewhelper(view)
1533 | view.ActiveViewport.ZoomBoundingBox(bbox)
1534 | scriptcontext.doc.Views.Redraw()
1535 |
1536 |
1537 | def ZoomExtents(view=None, all=False):
1538 | """Zooms to extents of visible objects in the specified view
1539 | Parameters:
1540 | view (str|guid, optional): title or id of the view. If omitted, current active view is used
1541 | all (bool, optional): zoom extents in all views
1542 | Returns:
1543 | None
1544 | Example:
1545 | import rhinoscriptsyntax as rs
1546 | rs.ZoomExtents()
1547 | See Also:
1548 | ZoomBoundingBox
1549 | ZoomSelected
1550 | """
1551 | if all:
1552 | views = scriptcontext.doc.Views.GetViewList(True, True)
1553 | for view in views: view.ActiveViewport.ZoomExtents()
1554 | else:
1555 | view = __viewhelper(view)
1556 | view.ActiveViewport.ZoomExtents()
1557 | scriptcontext.doc.Views.Redraw()
1558 |
1559 |
1560 | def ZoomSelected(view=None, all=False):
1561 | """Zoom to extents of selected objects in a view
1562 | Parameters:
1563 | view (str|guid, optional): title or id of the view. If omitted, active view is used
1564 | all (bool, optional): zoom extents in all views
1565 | Returns:
1566 | None
1567 | Example:
1568 | import rhinocriptsyntax as rs
1569 | obj = rs.GetObject("Select object", select=True)
1570 | if obj: rs.ZoomSelected()
1571 | See Also:
1572 | ZoomBoundingBox
1573 | ZoomExtents
1574 | """
1575 | if all:
1576 | views = scriptcontext.doc.Views.GetViewList(True, True)
1577 | for view in views: view.ActiveViewport.ZoomExtentsSelected()
1578 | else:
1579 | view = __viewhelper(view)
1580 | view.ActiveViewport.ZoomExtentsSelected()
1581 | scriptcontext.doc.Views.Redraw()
1582 |
```
--------------------------------------------------------------------------------
/rhino_mcp_server/static/object.py:
--------------------------------------------------------------------------------
```python
1 | import math
2 |
3 | import System
4 |
5 | import Rhino
6 |
7 | import scriptcontext
8 |
9 | from rhinoscript import utility as rhutil
10 | from rhinoscript.layer import __getlayer
11 | from rhinoscript.view import __viewhelper
12 |
13 |
14 | __ALLOWED_TRANSFORM_TYPES = [
15 | Rhino.Geometry.Point3d,
16 | Rhino.Geometry.Line,
17 | Rhino.Geometry.Rectangle3d,
18 | Rhino.Geometry.Circle,
19 | Rhino.Geometry.Ellipse,
20 | Rhino.Geometry.Arc,
21 | Rhino.Geometry.Polyline,
22 | Rhino.Geometry.Box,
23 | Rhino.Geometry.Sphere
24 | ]
25 |
26 |
27 | def CopyObject(object_id, translation=None):
28 | """Copies object from one location to another, or in-place.
29 | Parameters:
30 | object_id (guid): object to copy
31 | translation (vector, optional): translation vector to apply
32 | Returns:
33 | guid: id for the copy if successful
34 | None: if not able to copy
35 | Example:
36 | import rhinoscriptsyntax as rs
37 | id = rs.GetObject("Select object to copy")
38 | if id:
39 | start = rs.GetPoint("Point to copy from")
40 | if start:
41 | end = rs.GetPoint("Point to copy to", start)
42 | if end:
43 | translation = end-start
44 | rs.CopyObject( id, translation )
45 | See Also:
46 | CopyObjects
47 | """
48 | rc = CopyObjects(object_id, translation)
49 | if rc: return rc[0]
50 |
51 |
52 | def CopyObjects(object_ids, translation=None):
53 | """Copies one or more objects from one location to another, or in-place.
54 | Parameters:
55 | object_ids ([guid, ...])list of objects to copy
56 | translation (vector, optional): list of three numbers or Vector3d representing
57 | translation vector to apply to copied set
58 | Returns:
59 | list(guid, ...): identifiers for the copies if successful
60 | Example:
61 | import rhinoscriptsyntax as rs
62 | objectIds = rs.GetObjects("Select objects to copy")
63 | if objectIds:
64 | start = rs.GetPoint("Point to copy from")
65 | if start:
66 | end = rs.GetPoint("Point to copy to", start)
67 | if end:
68 | translation = end-start
69 | rs.CopyObjects( objectIds, translation )
70 | See Also:
71 | CopyObject
72 | """
73 | if translation:
74 | translation = rhutil.coerce3dvector(translation, True)
75 | translation = Rhino.Geometry.Transform.Translation(translation)
76 | else:
77 | translation = Rhino.Geometry.Transform.Identity
78 | return TransformObjects(object_ids, translation, True)
79 |
80 |
81 | def DeleteObject(object_id):
82 | """Deletes a single object from the document
83 | Parameters:
84 | object_id (guid): identifier of object to delete
85 | Returns:
86 | bool: True of False indicating success or failure
87 | Example:
88 | import rhinoscriptsyntax as rs
89 | id = rs.GetObject("Select object to delete")
90 | if id: rs.DeleteObject(id)
91 | See Also:
92 | DeleteObjects
93 | """
94 | object_id = rhutil.coerceguid(object_id, True)
95 | rc = scriptcontext.doc.Objects.Delete(object_id, True)
96 | if rc: scriptcontext.doc.Views.Redraw()
97 | return rc
98 |
99 |
100 | def DeleteObjects(object_ids):
101 | """Deletes one or more objects from the document
102 | Parameters:
103 | object_ids ([guid, ...]): identifiers of objects to delete
104 | Returns:
105 | number: Number of objects deleted
106 | Example:
107 | import rhinoscriptsyntax as rs
108 | object_ids = rs.GetObjects("Select objects to delete")
109 | if object_ids: rs.DeleteObjects(object_ids)
110 | See Also:
111 | DeleteObject
112 | """
113 | rc = 0
114 | id = rhutil.coerceguid(object_ids, False)
115 | if id: object_ids = [id]
116 | for id in object_ids:
117 | id = rhutil.coerceguid(id, True)
118 | if scriptcontext.doc.Objects.Delete(id, True): rc+=1
119 | if rc: scriptcontext.doc.Views.Redraw()
120 | return rc
121 |
122 |
123 | def FlashObject(object_ids, style=True):
124 | """Causes the selection state of one or more objects to change momentarily
125 | so the object appears to flash on the screen
126 | Parameters:
127 | object_ids ([guid, ...]) identifiers of objects to flash
128 | style (bool, optional): If True, flash between object color and selection color.
129 | If False, flash between visible and invisible
130 | Returns:
131 | None
132 | Example:
133 | import rhinoscriptsyntax as rs
134 | objs = rs.ObjectsByLayer("Default")
135 | if objs: rs.FlashObject(objs)
136 | See Also:
137 | HideObjects
138 | SelectObjects
139 | ShowObjects
140 | UnselectObjects
141 | """
142 | id = rhutil.coerceguid(object_ids, False)
143 | if id: object_ids = [id]
144 | rhobjs = [rhutil.coercerhinoobject(id, True, True) for id in object_ids]
145 | if rhobjs: scriptcontext.doc.Views.FlashObjects(rhobjs, style)
146 |
147 |
148 | def HideObject(object_id):
149 | """Hides a single object
150 | Parameters:
151 | object_id (guid): id of object to hide
152 | Returns:
153 | bool: True of False indicating success or failure
154 | Example:
155 | import rhinoscriptsyntax as rs
156 | id = rs.GetObject("Select object to hide")
157 | if id: rs.HideObject(id)
158 | See Also:
159 | HideObjects
160 | IsObjectHidden
161 | ShowObject
162 | ShowObjects
163 | """
164 | return HideObjects(object_id)==1
165 |
166 |
167 | def HideObjects(object_ids):
168 | """Hides one or more objects
169 | Parameters:
170 | object_ids ([guid, ...]): identifiers of objects to hide
171 | Returns:
172 | number: Number of objects hidden
173 | Example:
174 | import rhinoscriptsyntax as rs
175 | ids = rs.GetObjects("Select objects to hide")
176 | if ids: rs.HideObjects(ids)
177 | See Also:
178 | HideObjects
179 | IsObjectHidden
180 | ShowObject
181 | ShowObjects
182 | """
183 | id = rhutil.coerceguid(object_ids, False)
184 | if id: object_ids = [id]
185 | rc = 0
186 | for id in object_ids:
187 | id = rhutil.coerceguid(id, True)
188 | if scriptcontext.doc.Objects.Hide(id, False): rc += 1
189 | if rc: scriptcontext.doc.Views.Redraw()
190 | return rc
191 |
192 |
193 | def IsLayoutObject(object_id):
194 | """Verifies that an object is in either page layout space or model space
195 | Parameters:
196 | object_id (guid): id of an object to test
197 | Returns:
198 | bool: True if the object is in page layout space
199 | bool: False if the object is in model space
200 | Example:
201 | import rhinoscriptsyntax as rs
202 | id = rs.GetObject("Select object")
203 | if id:
204 | if rs.IsLayoutObject(id):
205 | print("The object is in page layout space.")
206 | else:
207 | print("The object is in model space.")
208 | See Also:
209 | IsObject
210 | IsObjectReference
211 | """
212 | rhobj = rhutil.coercerhinoobject(object_id, True, True)
213 | return rhobj.Attributes.Space == Rhino.DocObjects.ActiveSpace.PageSpace
214 |
215 |
216 | def IsObject(object_id):
217 | """Verifies the existence of an object
218 | Parameters:
219 | object_id (guid): an object to test
220 | Returns:
221 | bool: True if the object exists
222 | bool: False if the object does not exist
223 | Example:
224 | import rhinoscriptsyntax as rs
225 | #Do something here...
226 | if rs.IsObject(id):
227 | print("The object exists.")
228 | else:
229 | print("The object does not exist.")
230 | See Also:
231 | IsObjectHidden
232 | IsObjectInGroup
233 | IsObjectLocked
234 | IsObjectNormal
235 | IsObjectReference
236 | IsObjectSelectable
237 | IsObjectSelected
238 | IsObjectSolid
239 | """
240 | return rhutil.coercerhinoobject(object_id, True, False) is not None
241 |
242 |
243 | def IsObjectHidden(object_id):
244 | """Verifies that an object is hidden. Hidden objects are not visible, cannot
245 | be snapped to, and cannot be selected
246 | Parameters:
247 | object_id (guid): The identifier of an object to test
248 | Returns:
249 | bool: True if the object is hidden
250 | bool: False if the object is not hidden
251 | Example:
252 | import rhinoscriptsyntax as rs
253 | # Do something here...
254 | if rs.IsObjectHidden(id):
255 | print("The object is hidden.")
256 | else:
257 | print("The object is not hidden.")
258 | See Also:
259 | IsObject
260 | IsObjectInGroup
261 | IsObjectLocked
262 | IsObjectNormal
263 | IsObjectReference
264 | IsObjectSelectable
265 | IsObjectSelected
266 | IsObjectSolid
267 | """
268 | rhobj = rhutil.coercerhinoobject(object_id, True, True)
269 | return rhobj.IsHidden
270 |
271 |
272 | def IsObjectInBox(object_id, box, test_mode=True):
273 | """Verifies an object's bounding box is inside of another bounding box
274 | Parameters:
275 | object_id (guid): identifier of an object to be tested
276 | box ([point, point, point, point, point, point, point, point]): bounding box to test for containment
277 | test_mode (bool, optional): If True, the object's bounding box must be contained by box
278 | If False, the object's bounding box must be contained by or intersect box
279 | Returns:
280 | bool: True if object is inside box
281 | bool: False is object is not inside box
282 | Example:
283 | import rhinoscriptsyntax as rs
284 | box = rs.GetBox()
285 | if box:
286 | rs.EnableRedraw(False)
287 | object_list = rs.AllObjects()
288 | for obj in object_list:
289 | if rs.IsObjectInBox(obj, box, False):
290 | rs.SelectObject( obj )
291 | rs.EnableRedraw( True )
292 | See Also:
293 | BoundingBox
294 | GetBox
295 | """
296 | rhobj = rhutil.coercerhinoobject(object_id, True, True)
297 | box = rhutil.coerceboundingbox(box, True)
298 | objbox = rhobj.Geometry.GetBoundingBox(True)
299 | if test_mode: return box.Contains(objbox)
300 | union = Rhino.Geometry.BoundingBox.Intersection(box, objbox)
301 | return union.IsValid
302 |
303 |
304 | def IsObjectInGroup(object_id, group_name=None):
305 | """Verifies that an object is a member of a group
306 | Parameters:
307 | object_id (guid): The identifier of an object
308 | group_name (str, optional): The name of a group. If omitted, the function
309 | verifies that the object is a member of any group
310 | Returns:
311 | bool: True if the object is a member of the specified group. If a group_name
312 | was not specified, the object is a member of some group.
313 | bool: False if the object is not a member of the specified group. If a
314 | group_name was not specified, the object is not a member of any group
315 | Example:
316 | import rhinoscriptsyntax as rs
317 | id = rs.GetObject("Select object")
318 | if id:
319 | name = rs.GetString("Group name")
320 | if name:
321 | result = rs.IsObjectInGroup(id, name)
322 | if result:
323 | print("The object belongs to the group.")
324 | else:
325 | print("The object does not belong to the group.")
326 | See Also:
327 | IsObject
328 | IsObjectHidden
329 | IsObjectLocked
330 | IsObjectNormal
331 | IsObjectReference
332 | IsObjectSelectable
333 | IsObjectSelected
334 | IsObjectSolid
335 | """
336 | rhobj = rhutil.coercerhinoobject(object_id, True, True)
337 | count = rhobj.GroupCount
338 | if count<1: return False
339 | if not group_name: return True
340 | index = scriptcontext.doc.Groups.Find(group_name)
341 | if index<0: raise ValueError("%s group does not exist"%group_name)
342 | group_ids = rhobj.GetGroupList()
343 | for id in group_ids:
344 | if id==index: return True
345 | return False
346 |
347 |
348 | def IsObjectLocked(object_id):
349 | """Verifies that an object is locked. Locked objects are visible, and can
350 | be snapped to, but cannot be selected
351 | Parameters:
352 | object_id (guid): The identifier of an object to be tested
353 | Returns:
354 | bool: True if the object is locked
355 | bool: False if the object is not locked
356 | Example:
357 | import rhinoscriptsyntax as rs
358 | # Do something here...
359 | if rs.IsObjectLocked(object):
360 | print("The object is locked.")
361 | else:
362 | print("The object is not locked.")
363 | See Also:
364 | IsObject
365 | IsObjectHidden
366 | IsObjectInGroup
367 | IsObjectNormal
368 | IsObjectReference
369 | IsObjectSelectable
370 | IsObjectSelected
371 | IsObjectSolid
372 | """
373 | rhobj = rhutil.coercerhinoobject(object_id, True, True)
374 | return rhobj.IsLocked
375 |
376 |
377 | def IsObjectNormal(object_id):
378 | """Verifies that an object is normal. Normal objects are visible, can be
379 | snapped to, and can be selected
380 | Parameters:
381 | object_id (guid): The identifier of an object to be tested
382 | Returns:
383 | bool: True if the object is normal
384 | bool: False if the object is not normal
385 | Example:
386 | import rhinoscriptsyntax as rs
387 | #Do something here...
388 | if rs.IsObjectNormal(object):
389 | print("The object is normal.")
390 | else:
391 | print("The object is not normal.")
392 | See Also:
393 | IsObject
394 | IsObjectHidden
395 | IsObjectInGroup
396 | IsObjectLocked
397 | IsObjectReference
398 | IsObjectSelectable
399 | IsObjectSelected
400 | IsObjectSolid
401 | """
402 | rhobj = rhutil.coercerhinoobject(object_id, True, True)
403 | return rhobj.IsNormal
404 |
405 |
406 | def IsObjectReference(object_id):
407 | """Verifies that an object is a reference object. Reference objects are
408 | objects that are not part of the current document
409 | Parameters:
410 | object_id (guid): The identifier of an object to test
411 | Returns:
412 | bool: True if the object is a reference object
413 | bool: False if the object is not a reference object
414 | Example:
415 | import rhinoscriptsyntax as rs
416 | id = rs.GetObject("Select object")
417 | if rs.IsObjectReference(id):
418 | print("The object is a reference object.")
419 | else:
420 | print("The object is not a reference object.")
421 | See Also:
422 | IsObject
423 | IsObjectHidden
424 | IsObjectInGroup
425 | IsObjectLocked
426 | IsObjectNormal
427 | IsObjectSelectable
428 | IsObjectSelected
429 | IsObjectSolid
430 | """
431 | rhobj = rhutil.coercerhinoobject(object_id, True, True)
432 | return rhobj.IsReference
433 |
434 |
435 | def IsObjectSelectable(object_id):
436 | """Verifies that an object can be selected
437 | Parameters:
438 | object_id (guid): The identifier of an object to test
439 | Returns:
440 | bool: True or False
441 | Example:
442 | import rhinoscriptsyntax as rs
443 | # Do something here...
444 | if rs.IsObjectSelectable(object):
445 | rs.SelectObject( object )
446 | See Also:
447 | IsObject
448 | IsObjectHidden
449 | IsObjectInGroup
450 | IsObjectLocked
451 | IsObjectNormal
452 | IsObjectReference
453 | IsObjectSelected
454 | IsObjectSolid
455 | """
456 | rhobj = rhutil.coercerhinoobject(object_id, True, True)
457 | return rhobj.IsSelectable(True,False,False,False)
458 |
459 |
460 | def IsObjectSelected(object_id):
461 | """Verifies that an object is currently selected.
462 | Parameters:
463 | object_id (guid): The identifier of an object to test
464 | Returns:
465 | int: 0, the object is not selected
466 | int: 1, the object is selected
467 | int: 2, the object is entirely persistently selected
468 | int: 3, one or more proper sub-objects are selected
469 | Example:
470 | import rhinoscriptsyntax as rs
471 | object = rs.GetObject()
472 | if rs.IsObjectSelected(object):
473 | print("The object is selected.")
474 | else:
475 | print("The object is not selected.")
476 | See Also:
477 | IsObject
478 | IsObjectHidden
479 | IsObjectInGroup
480 | IsObjectLocked
481 | IsObjectNormal
482 | IsObjectReference
483 | IsObjectSelectable
484 | IsObjectSolid
485 | """
486 | rhobj = rhutil.coercerhinoobject(object_id, True, True)
487 | return rhobj.IsSelected(False)
488 |
489 |
490 | def IsObjectSolid(object_id):
491 | """Determines if an object is closed, solid
492 | Parameters:
493 | object_id (guid): The identifier of an object to test
494 | Returns:
495 | bool: True if the object is solid, or a mesh is closed.
496 | bool: False otherwise.
497 | Example:
498 | import rhinoscriptsyntax as rs
499 | id = rs.GetObject("Select object")
500 | if rs.IsObjectSolid(id):
501 | print("The object is solid.")
502 | else:
503 | print("The object is not solid.")
504 | See Also:
505 | IsObject
506 | IsObjectHidden
507 | IsObjectInGroup
508 | IsObjectLocked
509 | IsObjectNormal
510 | IsObjectReference
511 | IsObjectSelectable
512 | IsObjectSelected
513 | """
514 | rhobj = rhutil.coercerhinoobject(object_id, True, True)
515 | return rhobj.IsSolid
516 |
517 |
518 | def IsObjectValid(object_id):
519 | """Verifies an object's geometry is valid and without error
520 | Parameters:
521 | object_id (guid): The identifier of an object to test
522 | Returns:
523 | bool: True if the object is valid
524 | Example:
525 | import rhinoscriptsyntax as rs
526 | id = rs.GetObject("Select object")
527 | if rs.IsObjectValid(id):
528 | print("The object is valid.")
529 | else:
530 | print("The object is not valid.")
531 | See Also:
532 | IsObject
533 | """
534 | rhobj = rhutil.coercerhinoobject(object_id, True, True)
535 | return rhobj.IsValid
536 |
537 |
538 | def IsVisibleInView(object_id, view=None):
539 | """Verifies an object is visible in a view
540 | Parameters:
541 | object_id (guid): the identifier of an object to test
542 | view (str, optional): he title of the view. If omitted, the current active view is used.
543 | Returns:
544 | bool: True if the object is visible in the specified view, otherwise False. None on error
545 | Example:
546 | import rhinoscriptsyntax as rs
547 | obj = rs.GetObject("Select object")
548 | if rs.IsObject(obj):
549 | view = rs.CurrentView()
550 | if rs.IsVisibleInView(obj, view):
551 | print("The object is visible in {}.".format(view))
552 | else:
553 | print("The object is not visible in {}.".format(view))
554 | See Also:
555 | IsObject
556 | IsView
557 | """
558 | rhobj = rhutil.coercerhinoobject(object_id, True, True)
559 | viewport = __viewhelper(view).MainViewport
560 | bbox = rhobj.Geometry.GetBoundingBox(True)
561 | return rhobj.Visible and viewport.IsVisible(bbox)
562 |
563 |
564 | def LockObject(object_id):
565 | """Locks a single object. Locked objects are visible, and they can be
566 | snapped to. But, they cannot be selected.
567 | Parameters:
568 | object_id (guid): The identifier of an object
569 | Returns:
570 | bool: True or False indicating success or failure
571 | Example:
572 | import rhinoscriptsyntax as rs
573 | id = rs.GetObject("Select object to lock")
574 | if id: rs.LockObject(id)
575 | See Also:
576 | IsObjectLocked
577 | LockObjects
578 | UnlockObject
579 | UnlockObjects
580 | """
581 | return LockObjects(object_id)==1
582 |
583 |
584 | def LockObjects(object_ids):
585 | """Locks one or more objects. Locked objects are visible, and they can be
586 | snapped to. But, they cannot be selected.
587 | Parameters:
588 | object_ids ([guid, ...]): list of Strings or Guids. The identifiers of objects
589 | Returns:
590 | number: number of objects locked
591 | Example:
592 | import rhinoscriptsyntax as rs
593 | ids = rs.GetObjects("Select objects to lock")
594 | if ids: rs.LockObjects(ids)
595 | See Also:
596 | IsObjectLocked
597 | LockObject
598 | UnlockObject
599 | UnlockObjects
600 | """
601 | id = rhutil.coerceguid(object_ids, False)
602 | if id: object_ids = [id]
603 | rc = 0
604 | for id in object_ids:
605 | id = rhutil.coerceguid(id, True)
606 | if scriptcontext.doc.Objects.Lock(id, False): rc += 1
607 | if rc: scriptcontext.doc.Views.Redraw()
608 | return rc
609 |
610 |
611 | def MatchObjectAttributes(target_ids, source_id=None):
612 | """Matches, or copies the attributes of a source object to a target object
613 | Parameters:
614 | target_ids ([guid, ...]): identifiers of objects to copy attributes to
615 | source_id (guid, optional): identifier of object to copy attributes from. If None,
616 | then the default attributes are copied to the target_ids
617 | Returns:
618 | number: number of objects modified
619 | Example:
620 | import rhinoscriptsyntax as rs
621 | targets = rs.GetObjects("Select objects")
622 | if targets:
623 | source = rs.GetObject("Select object to match")
624 | if source: rs.MatchObjectAttributes( targets, source )
625 | See Also:
626 | GetObject
627 | GetObjects
628 | """
629 | id = rhutil.coerceguid(target_ids, False)
630 | if id: target_ids = [id]
631 | source_attr = Rhino.DocObjects.ObjectAttributes()
632 | if source_id:
633 | source = rhutil.coercerhinoobject(source_id, True, True)
634 | source_attr = source.Attributes.Duplicate()
635 | rc = 0
636 | for id in target_ids:
637 | id = rhutil.coerceguid(id, True)
638 | if scriptcontext.doc.Objects.ModifyAttributes(id, source_attr, True):
639 | rc += 1
640 | if rc: scriptcontext.doc.Views.Redraw()
641 | return rc
642 |
643 |
644 | def MirrorObject(object_id, start_point, end_point, copy=False):
645 | """Mirrors a single object
646 | Parameters:
647 | object_id (guid): The identifier of an object to mirror
648 | start_point (point): start of the mirror plane
649 | end_point (point): end of the mirror plane
650 | copy (bool, optional): copy the object
651 | Returns:
652 | guid: Identifier of the mirrored object if successful
653 | None: on error
654 | Example:
655 | import rhinoscriptsyntax as rs
656 | obj = rs.GetObject("Select object to mirror")
657 | if obj:
658 | start = rs.GetPoint("Start of mirror plane")
659 | end = rs.GetPoint("End of mirror plane")
660 | if start and end:
661 | rs.MirrorObject( obj, start, end, True )
662 | See Also:
663 | MirrorObjects
664 | """
665 | rc = MirrorObjects(object_id, start_point, end_point, copy)
666 | if rc: return rc[0]
667 |
668 |
669 | def MirrorObjects(object_ids, start_point, end_point, copy=False):
670 | """Mirrors a list of objects
671 | Parameters:
672 | object_ids ([guid, ...]): identifiers of objects to mirror
673 | start_point (point): start of the mirror plane
674 | end_point (point): end of the mirror plane
675 | copy (bool, optional): copy the objects
676 | Returns:
677 | list(guid, ...): List of identifiers of the mirrored objects if successful
678 | Example:
679 | import rhinoscriptsyntax as rs
680 | objs = rs.GetObjects("Select objects to mirror")
681 | if objs:
682 | start = rs.GetPoint("Start of mirror plane")
683 | end = rs.GetPoint("End of mirror plane")
684 | if start and end:
685 | rs.MirrorObjects( objs, start, end, True )
686 | See Also:
687 | MirrorObject
688 | """
689 | start_point = rhutil.coerce3dpoint(start_point, True)
690 | end_point = rhutil.coerce3dpoint(end_point, True)
691 | vec = end_point-start_point
692 | if vec.IsTiny(0): raise Exception("start and end points are too close to each other")
693 | normal = scriptcontext.doc.Views.ActiveView.ActiveViewport.ConstructionPlane().Normal
694 | vec = Rhino.Geometry.Vector3d.CrossProduct(vec, normal)
695 | vec.Unitize()
696 | xf = Rhino.Geometry.Transform.Mirror(start_point, vec)
697 | rc = TransformObjects(object_ids, xf, copy)
698 | return rc
699 |
700 |
701 | def MoveObject(object_id, translation):
702 | """Moves a single object
703 | Parameters:
704 | object_id (guid): The identifier of an object to move
705 | translation (vector): list of 3 numbers or Vector3d
706 | Returns:
707 | guid: Identifier of the moved object if successful
708 | None: on error
709 | Example:
710 | import rhinoscriptsyntax as rs
711 | id = rs.GetObject("Select object to move")
712 | if id:
713 | start = rs.GetPoint("Point to move from")
714 | if start:
715 | end = rs.GetPoint("Point to move to")
716 | if end:
717 | translation = end-start
718 | rs.MoveObject(id, translation)
719 | See Also:
720 | MoveObjects
721 | """
722 | rc = MoveObjects(object_id, translation)
723 | if rc: return rc[0]
724 |
725 |
726 | def MoveObjects(object_ids, translation):
727 | """Moves one or more objects
728 | Parameters:
729 | object_ids ([guid, ...]): The identifiers objects to move
730 | translation (vector): list of 3 numbers or Vector3d
731 | Returns:
732 | list(guid, ...): identifiers of the moved objects if successful
733 | Example:
734 | import rhinoscriptsyntax as rs
735 | ids = rs.GetObjects("Select objects to move")
736 | if ids:
737 | start = rs.GetPoint("Point to move from")
738 | if start:
739 | end = rs.GetPoint("Point to move to")
740 | if end:
741 | translation = end-start
742 | rs.MoveObjects( ids, translation )
743 | See Also:
744 | MoveObject
745 | """
746 | translation = rhutil.coerce3dvector(translation, True)
747 | xf = Rhino.Geometry.Transform.Translation(translation)
748 | rc = TransformObjects(object_ids, xf)
749 | return rc
750 |
751 |
752 | def ObjectColor(object_ids, color=None):
753 | """Returns of modifies the color of an object. Object colors are represented
754 | as RGB colors. An RGB color specifies the relative intensity of red, green,
755 | and blue to cause a specific color to be displayed
756 | Parameters:
757 | object_ids ([guid, ...]): id or ids of object(s)
758 | color (color, optional): the new color value. If omitted, then current object
759 | color is returned. If object_ids is a list, color is required
760 | Returns:
761 | color: If color value is not specified, the current color value
762 | color: If color value is specified, the previous color value
763 | number: If object_ids is a list, then the number of objects modified
764 | Example:
765 | import rhinoscriptsyntax as rs
766 | objs = rs.GetObjects("Select objects to change color")
767 | if objs:
768 | color = rs.GetColor(0)
769 | if color:
770 | for obj in objs: rs.ObjectColor( obj, color )
771 | See Also:
772 | ObjectColorSource
773 | ObjectsByColor
774 | """
775 | id = rhutil.coerceguid(object_ids, False)
776 | rhino_object = None
777 | rhino_objects = None
778 | if id:
779 | rhino_object = rhutil.coercerhinoobject(id, True, True)
780 | else:
781 | rhino_objects = [rhutil.coercerhinoobject(id, True, True) for id in object_ids]
782 | if len(rhino_objects)==1:
783 | rhino_object = rhino_objects[0]
784 | rhino_objects = None
785 | if color is None:
786 | #get the color
787 | if rhino_objects: raise ValueError("color must be specified when a list of rhino objects is provided")
788 | return rhino_object.Attributes.DrawColor(scriptcontext.doc)
789 | color = rhutil.coercecolor(color, True)
790 | if rhino_objects is not None:
791 | for rh_obj in rhino_objects:
792 | attr = rh_obj.Attributes
793 | attr.ObjectColor = color
794 | attr.ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject
795 | scriptcontext.doc.Objects.ModifyAttributes( rh_obj, attr, True)
796 | scriptcontext.doc.Views.Redraw()
797 | return len(rhino_objects)
798 | rc = rhino_object.Attributes.DrawColor(scriptcontext.doc)
799 | attr = rhino_object.Attributes
800 | attr.ObjectColor = color
801 | attr.ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject
802 | scriptcontext.doc.Objects.ModifyAttributes( rhino_object, attr, True )
803 | scriptcontext.doc.Views.Redraw()
804 | return rc
805 |
806 |
807 | def ObjectColorSource(object_ids, source=None):
808 | """Returns of modifies the color source of an object.
809 | Parameters:
810 | object_ids ([guid, ...]): single identifier of list of identifiers
811 | source (number, optional) = new color source
812 | 0 = color from layer
813 | 1 = color from object
814 | 2 = color from material
815 | 3 = color from parent
816 | Returns:
817 | if color source is not specified, the current color source
818 | is color source is specified, the previous color source
819 | if color_ids is a list, then the number of objects modifief
820 | Example:
821 | import rhinoscriptsyntax as rs
822 | objs = rs.GetObjects("Select objects to reset color source")
823 | if objs:
824 | for obj In objs: rs.ObjectColorSource(obj, 0)
825 | See Also:
826 | ObjectColor
827 | """
828 | id = rhutil.coerceguid(object_ids, False)
829 | if id:
830 | rhobj = rhutil.coercerhinoobject(id, True, True)
831 | rc = int(rhobj.Attributes.ColorSource)
832 | if source is not None:
833 | rhobj.Attributes.ColorSource = System.Enum.ToObject(Rhino.DocObjects.ObjectColorSource, source)
834 | rhobj.CommitChanges()
835 | scriptcontext.doc.Views.Redraw()
836 | return rc
837 | else:
838 | rc = 0
839 | source = System.Enum.ToObject(Rhino.DocObjects.ObjectColorSource, source)
840 | for id in object_ids:
841 | rhobj = rhutil.coercerhinoobject(id, True, True)
842 | rhobj.Attributes.ColorSource = source
843 | rhobj.CommitChanges()
844 | rc += 1
845 | if rc: scriptcontext.doc.Views.Redraw()
846 | return rc
847 |
848 |
849 | def ObjectDescription(object_id):
850 | """Returns a short text description of an object
851 | Parameters:
852 | object_id = identifier of an object
853 | Returns:
854 | A short text description of the object if successful.
855 | Example:
856 | import rhinoscriptsyntax as rs
857 | obj = rs.GetObject("Select object")
858 | if obj:
859 | description = rs.ObjectDescription(obj)
860 | print("Object description:"{} .format(description))
861 | See Also:
862 | ObjectType
863 | """
864 | rhobj = rhutil.coercerhinoobject(object_id, True, True)
865 | return rhobj.ShortDescription(False)
866 |
867 |
868 | def ObjectGroups(object_id):
869 | """Returns all of the group names that an object is assigned to
870 | Parameters:
871 | object_id ([guid, ...]): identifier of an object(s)
872 | Returns:
873 | list(str, ...): list of group names on success
874 | Example:
875 | import rhinoscriptsyntax as rs
876 | obj = rs.GetObject("Select object")
877 | if obj:
878 | groups = rs.ObjectGroups(obj)
879 | if groups:
880 | for group in groups: print("Object group: {}".format(group))
881 | else:
882 | print("No groups.")
883 | See Also:
884 | ObjectsByGroup
885 | """
886 | rhino_object = rhutil.coercerhinoobject(object_id, True, True)
887 | if rhino_object.GroupCount<1: return []
888 | group_indices = rhino_object.GetGroupList()
889 | rc = [scriptcontext.doc.Groups.GroupName(index) for index in group_indices]
890 | return rc
891 |
892 |
893 | def ObjectLayer(object_id, layer=None):
894 | """Returns or modifies the layer of an object
895 | Parameters:
896 | object_id ([guid, ...]) the identifier of the object(s)
897 | layer (str, optional): name of an existing layer
898 | Returns:
899 | str: If a layer is not specified, the object's current layer
900 | str: If a layer is specified, the object's previous layer
901 | number: If object_id is a list or tuple, the number of objects modified
902 | Example:
903 | import rhinoscriptsyntax as rs
904 | id = rs.GetObject("Select object")
905 | if id: rs.ObjectLayer(id, "Default")
906 | See Also:
907 | ObjectsByLayer
908 | """
909 | if type(object_id) is not str and hasattr(object_id, "__len__"):
910 | layer = __getlayer(layer, True)
911 | index = layer.LayerIndex
912 | for id in object_id:
913 | obj = rhutil.coercerhinoobject(id, True, True)
914 | obj.Attributes.LayerIndex = index
915 | obj.CommitChanges()
916 | scriptcontext.doc.Views.Redraw()
917 | return len(object_id)
918 | obj = rhutil.coercerhinoobject(object_id, True, True)
919 | if obj is None: return scriptcontext.errorhandler()
920 | index = obj.Attributes.LayerIndex
921 | rc = scriptcontext.doc.Layers[index].FullPath
922 | if layer:
923 | layer = __getlayer(layer, True)
924 | index = layer.LayerIndex
925 | obj.Attributes.LayerIndex = index
926 | obj.CommitChanges()
927 | scriptcontext.doc.Views.Redraw()
928 | return rc
929 |
930 |
931 | def ObjectLayout(object_id, layout=None, return_name=True):
932 | """Returns or changes the layout or model space of an object
933 | Parameters:
934 | object_id (guid): identifier of the object
935 | layout (str|guid, optional): to change, or move, an object from model space to page
936 | layout space, or from one page layout to another, then specify the
937 | title or identifier of an existing page layout view. To move an object
938 | from page layout space to model space, just specify None
939 | return_name[opt] = If True, the name, or title, of the page layout view
940 | is returned. If False, the identifier of the page layout view is returned
941 | Returns:
942 | str: if layout is not specified, the object's current page layout view
943 | str: if layout is specified, the object's previous page layout view
944 | None: if not successful
945 | Example:
946 | import rhinoscriptsyntax as rs
947 | obj = rs.GetObject("Select object")
948 | if obj: rs.ObjectLayout(obj, "Page 1")
949 | See Also:
950 | IsLayoutObject
951 | IsLayout
952 | ViewNames
953 | """
954 | rhobj = rhutil.coercerhinoobject(object_id, True, True)
955 | rc = None
956 | if rhobj.Attributes.Space==Rhino.DocObjects.ActiveSpace.PageSpace:
957 | page_id = rhobj.Attributes.ViewportId
958 | pageview = scriptcontext.doc.Views.Find(page_id)
959 | if return_name: rc = pageview.MainViewport.Name
960 | else: rc = pageview.MainViewport.Id
961 | if layout is None: #move to model space
962 | rhobj.Attributes.Space = Rhino.DocObjects.ActiveSpace.ModelSpace
963 | rhobj.Attributes.ViewportId = System.Guid.Empty
964 | rhobj.CommitChanges()
965 | scriptcontext.doc.Views.Redraw()
966 | else:
967 | if layout:
968 | layout = scriptcontext.doc.Views.Find(layout, False)
969 | if layout is not None and isinstance(layout, Rhino.Display.RhinoPageView):
970 | rhobj.Attributes.ViewportId = layout.MainViewport.Id
971 | rhobj.Attributes.Space = Rhino.DocObjects.ActiveSpace.PageSpace
972 | rhobj.CommitChanges()
973 | scriptcontext.doc.Views.Redraw()
974 | return rc
975 |
976 |
977 | def ObjectLinetype(object_ids, linetype=None):
978 | """Returns of modifies the linetype of an object
979 | Parameters:
980 | object_ids ({guid, ...]): identifiers of object(s)
981 | linetype (str, optional): name of an existing linetype. If omitted, the current
982 | linetype is returned. If object_ids is a list of identifiers, this parameter
983 | is required
984 | Returns:
985 | str: If a linetype is not specified, the object's current linetype
986 | str: If linetype is specified, the object's previous linetype
987 | number: If object_ids is a list, the number of objects modified
988 | Example:
989 | import rhinoscriptsyntax as rs
990 | obj = rs.GetObject("Select object")
991 | if obj: rs.ObjectLinetype(obj, "Continuous")
992 | See Also:
993 | ObjectLinetypeSource
994 | """
995 | id = rhutil.coerceguid(object_ids, False)
996 | if id:
997 | rhino_object = rhutil.coercerhinoobject(id, True, True)
998 | oldindex = scriptcontext.doc.Linetypes.LinetypeIndexForObject(rhino_object)
999 | if linetype:
1000 | newindex = scriptcontext.doc.Linetypes.Find(linetype)
1001 | rhino_object.Attributes.LinetypeSource = Rhino.DocObjects.ObjectLinetypeSource.LinetypeFromObject
1002 | rhino_object.Attributes.LinetypeIndex = newindex
1003 | rhino_object.CommitChanges()
1004 | scriptcontext.doc.Views.Redraw()
1005 | return scriptcontext.doc.Linetypes[oldindex].Name
1006 |
1007 | newindex = scriptcontext.doc.Linetypes.Find(linetype)
1008 | if newindex<0: raise Exception("%s does not exist in LineTypes table"%linetype)
1009 | for id in object_ids:
1010 | rhino_object = rhutil.coercerhinoobject(id, True, True)
1011 | rhino_object.Attributes.LinetypeSource = Rhino.DocObjects.ObjectLinetypeSource.LinetypeFromObject
1012 | rhino_object.Attributes.LinetypeIndex = newindex
1013 | rhino_object.CommitChanges()
1014 | scriptcontext.doc.Views.Redraw()
1015 | return len(object_ids)
1016 |
1017 |
1018 | def ObjectLinetypeSource(object_ids, source=None):
1019 | """Returns of modifies the linetype source of an object
1020 | Parameters:
1021 | object_ids ([guid, ...]): identifiers of object(s)
1022 | source (number, optional): new linetype source. If omitted, the current source is returned.
1023 | If object_ids is a list of identifiers, this parameter is required
1024 | 0 = By Layer
1025 | 1 = By Object
1026 | 3 = By Parent
1027 | Returns:
1028 | number: If a source is not specified, the object's current linetype source
1029 | number: If source is specified, the object's previous linetype source
1030 | number: If object_ids is a list, the number of objects modified
1031 | Example:
1032 | import rhinoscriptsyntax as rs
1033 | objects = rs.GetObjects("Select objects to reset linetype source")
1034 | if objects:
1035 | for obj in objects: rs.ObjectLinetypeSource( obj, 0 )
1036 | See Also:
1037 | ObjectLinetype
1038 | """
1039 | id = rhutil.coerceguid(object_ids, False)
1040 | if id:
1041 | rhino_object = rhutil.coercerhinoobject(id, True, True)
1042 | oldsource = rhino_object.Attributes.LinetypeSource
1043 | if source is not None:
1044 | source = System.Enum.ToObject(Rhino.DocObjects.ObjectLinetypeSource, source)
1045 | rhino_object.Attributes.LinetypeSource = source
1046 | rhino_object.CommitChanges()
1047 | scriptcontext.doc.Views.Redraw()
1048 | return int(oldsource)
1049 | source = System.Enum.ToObject(Rhino.DocObjects.ObjectLinetypeSource, source)
1050 | for id in object_ids:
1051 | rhino_object = rhutil.coercerhinoobject(id, True, True)
1052 | rhino_object.Attributes.LinetypeSource = source
1053 | rhino_object.CommitChanges()
1054 | scriptcontext.doc.Views.Redraw()
1055 | return len(object_ids)
1056 |
1057 |
1058 | def ObjectMaterialIndex(object_id, material_index=None):
1059 | """Returns or changes the material index of an object. Rendering materials are stored in
1060 | Rhino's rendering material table. The table is conceptually an array. Render
1061 | materials associated with objects and layers are specified by zero based
1062 | indices into this array.
1063 | Parameters:
1064 | object_id (guid): identifier of an object
1065 | index (number, optional): the new material index
1066 | Returns:
1067 | number: If the return value of ObjectMaterialSource is "material by object", then
1068 | the return value of this function is the index of the object's rendering
1069 | material. A material index of -1 indicates no material has been assigned,
1070 | and that Rhino's internal default material has been assigned to the object.
1071 | None: on failure
1072 | Example:
1073 | import rhinoscriptsyntax as rs
1074 | obj = rs.GetObject("Select object")
1075 | if obj:
1076 | source = rs.ObjectMaterialSource(obj)
1077 | if source==0:
1078 | print("The material source is by layer")
1079 | else:
1080 | print("The material source is by object")
1081 | index = rs.ObjectMaterialIndex(obj)
1082 | if index==-1: print("The material is default.")
1083 | else: print("The material is custom.")
1084 | See Also:
1085 | ObjectMaterialSource
1086 | """
1087 | rhino_object = rhutil.coercerhinoobject(object_id, True, True)
1088 | if material_index is not None and material_index < scriptcontext.doc.Materials.Count:
1089 | attrs = rhino_object.Attributes
1090 | attrs.MaterialIndex = material_index
1091 | scriptcontext.doc.Objects.ModifyAttributes(rhino_object, attrs, True)
1092 | return rhino_object.Attributes.MaterialIndex
1093 |
1094 |
1095 | def ObjectMaterialSource(object_ids, source=None):
1096 | """Returns or modifies the rendering material source of an object.
1097 | Parameters:
1098 | object_ids ([guid, ...]): one or more object identifiers
1099 | source (number, optional): The new rendering material source. If omitted and a single
1100 | object is provided in object_ids, then the current material source is
1101 | returned. This parameter is required if multiple objects are passed in
1102 | object_ids
1103 | 0 = Material from layer
1104 | 1 = Material from object
1105 | 3 = Material from parent
1106 | Returns:
1107 | number: If source is not specified, the current rendering material source
1108 | number: If source is specified, the previous rendering material source
1109 | number: If object_ids refers to multiple objects, the number of objects modified
1110 | Example:
1111 | import rhinoscriptsyntax as rs
1112 | objects = rs.GetObjects("Select objects to reset rendering material source")
1113 | if objects:
1114 | [rs.ObjectMaterialSource(obj, 0) for obj in objects]
1115 | See Also:
1116 | ObjectMaterialIndex
1117 | """
1118 | id = rhutil.coerceguid(object_ids, False)
1119 | if id: # working with single object
1120 | rhino_object = rhutil.coercerhinoobject(id, True, True)
1121 | rc = int(rhino_object.Attributes.MaterialSource)
1122 | if source is not None:
1123 | rhino_object.Attributes.MaterialSource = System.Enum.ToObject(Rhino.DocObjects.ObjectMaterialSource, source)
1124 | rhino_object.CommitChanges()
1125 | return rc
1126 | # else working with multiple objects
1127 | if source is None: raise Exception("source is required when object_ids represents multiple objects")
1128 | source = System.Enum.ToObject(Rhino.DocObjects.ObjectMaterialSource, source)
1129 | for id in object_ids:
1130 | rhino_object = rhutil.coercerhinoobject(id, True, True)
1131 | rhino_object.Attributes.MaterialSource = source
1132 | rhino_object.CommitChanges()
1133 | return len(object_ids)
1134 |
1135 |
1136 | def ObjectName(object_id, name=None):
1137 | """Returns or modifies the name of an object
1138 | Parameters:
1139 | object_id ([guid, ...]): id or ids of object(s)
1140 | name (str, optional): the new object name. If omitted, the current name is returned
1141 | Returns:
1142 | str: If name is not specified, the current object name
1143 | str: If name is specified, the previous object name
1144 | number: If object_id is a list, the number of objects changed
1145 | Example:
1146 | import rhinoscriptsyntax as rs
1147 | points = rs.GetPoints(message1="Pick some points")
1148 | if points:
1149 | count = 0
1150 | for point in points:
1151 | obj = rs.AddPoint(point)
1152 | if obj:
1153 | rs.ObjectName( obj, "Point"+str(count) )
1154 | count += 1
1155 | See Also:
1156 | ObjectsByName
1157 | """
1158 | id = rhutil.coerceguid(object_id, False)
1159 | rhino_object = None
1160 | rhino_objects = None
1161 | if id:
1162 | rhino_object = rhutil.coercerhinoobject(id, True, True)
1163 | else:
1164 | rhino_objects = [rhutil.coercerhinoobject(id, True, True) for id in object_id]
1165 | if not rhino_objects: return 0
1166 | if len(rhino_objects)==1:
1167 | rhino_object = rhino_objects[0]
1168 | rhino_objects = None
1169 | if name is None: #get the name
1170 | if rhino_objects: raise Exception("name required when object_id represents multiple objects")
1171 | return rhino_object.Name
1172 | if rhino_objects:
1173 | for rh_obj in rhino_objects:
1174 | attr = rh_obj.Attributes
1175 | attr.Name = name
1176 | scriptcontext.doc.Objects.ModifyAttributes(rh_obj, attr, True)
1177 | return len(rhino_objects)
1178 | rc = rhino_object.Name
1179 | if not type(name) is str: name = str(name)
1180 | rhino_object.Attributes.Name = name
1181 | rhino_object.CommitChanges()
1182 | return rc
1183 |
1184 |
1185 | def ObjectPrintColor(object_ids, color=None):
1186 | """Returns or modifies the print color of an object
1187 | Parameters:
1188 | object_ids ([guid, ...]): identifiers of object(s)
1189 | color (color, optional): new print color. If omitted, the current color is returned.
1190 | Returns:
1191 | color: If color is not specified, the object's current print color
1192 | color: If color is specified, the object's previous print color
1193 | number: If object_ids is a list or tuple, the number of objects modified
1194 | Example:
1195 | import rhinoscriptsyntax as rs
1196 | objects = rs.GetObjects("Select objects to change print color")
1197 | if objects:
1198 | color = rs.GetColor()
1199 | if color:
1200 | for object in objects: rs.ObjectPrintColor(object, color)
1201 | See Also:
1202 | ObjectPrintColorSource
1203 | """
1204 | id = rhutil.coerceguid(object_ids, False)
1205 | if id:
1206 | rhino_object = rhutil.coercerhinoobject(id, True, True)
1207 | rc = rhino_object.Attributes.PlotColor
1208 | if color:
1209 | rhino_object.Attributes.PlotColorSource = Rhino.DocObjects.ObjectPlotColorSource.PlotColorFromObject
1210 | rhino_object.Attributes.PlotColor = rhutil.coercecolor(color, True)
1211 | rhino_object.CommitChanges()
1212 | scriptcontext.doc.Views.Redraw()
1213 | return rc
1214 | for id in object_ids:
1215 | color = rhutil.coercecolor(color, True)
1216 | rhino_object = rhutil.coercerhinoobject(id, True, True)
1217 | rhino_object.Attributes.PlotColorSource = Rhino.DocObjects.ObjectPlotColorSource.PlotColorFromObject
1218 | rhino_object.Attributes.PlotColor = color
1219 | rhino_object.CommitChanges()
1220 | scriptcontext.doc.Views.Redraw()
1221 | return len(object_ids)
1222 |
1223 |
1224 | def ObjectPrintColorSource(object_ids, source=None):
1225 | """Returns or modifies the print color source of an object
1226 | Parameters:
1227 | object_ids ([guid, ...]): identifiers of object(s)
1228 | source (number, optional): new print color source
1229 | 0 = print color by layer
1230 | 1 = print color by object
1231 | 3 = print color by parent
1232 | Returns:
1233 | number: If source is not specified, the object's current print color source
1234 | number: If source is specified, the object's previous print color source
1235 | number: If object_ids is a list or tuple, the number of objects modified
1236 | Example:
1237 | import rhinoscriptsyntax as rs
1238 | objects = rs.GetObjects("Select objects to reset print color source")
1239 | if objects:
1240 | for object in objects: rs.ObjectPrintColorSource(object, 0)
1241 | See Also:
1242 | ObjectPrintColor
1243 | """
1244 | id = rhutil.coerceguid(object_ids, False)
1245 | if id:
1246 | rhino_object = rhutil.coercerhinoobject(id, True, True)
1247 | rc = int(rhino_object.Attributes.PlotColorSource)
1248 | if source is not None:
1249 | rhino_object.Attributes.PlotColorSource = System.Enum.ToObject(Rhino.DocObjects.ObjectPlotColorSource, source)
1250 | rhino_object.CommitChanges()
1251 | scriptcontext.doc.Views.Redraw()
1252 | return rc
1253 | for id in object_ids:
1254 | rhino_object = rhutil.coercerhinoobject(id, True, True)
1255 | rhino_object.Attributes.PlotColorSource = System.Enum.ToObject(Rhino.DocObjects.ObjectPlotColorSource, source)
1256 | rhino_object.CommitChanges()
1257 | scriptcontext.doc.Views.Redraw()
1258 | return len(object_ids)
1259 |
1260 |
1261 | def ObjectPrintWidth(object_ids, width=None):
1262 | """Returns or modifies the print width of an object
1263 | Parameters:
1264 | object_ids ([guid, ...]): identifiers of object(s)
1265 | width (number, optional): new print width value in millimeters, where width=0 means use
1266 | the default width, and width<0 means do not print (visible for screen display,
1267 | but does not show on print). If omitted, the current width is returned.
1268 | Returns:
1269 | number: If width is not specified, the object's current print width
1270 | number: If width is specified, the object's previous print width
1271 | number: If object_ids is a list or tuple, the number of objects modified
1272 | Example:
1273 | import rhinoscriptsyntax as rs
1274 | objs = rs.GetObjects("Select objects to change print width")
1275 | if objs:
1276 | for obj in objs: rs.ObjectPrintWidth(obj,0.5)
1277 | See Also:
1278 | ObjectPrintWidthSource
1279 | """
1280 | id = rhutil.coerceguid(object_ids, False)
1281 | if id:
1282 | rhino_object = rhutil.coercerhinoobject(id, True, True)
1283 | rc = rhino_object.Attributes.PlotWeight
1284 | if width is not None:
1285 | rhino_object.Attributes.PlotWeightSource = Rhino.DocObjects.ObjectPlotWeightSource.PlotWeightFromObject
1286 | rhino_object.Attributes.PlotWeight = width
1287 | rhino_object.CommitChanges()
1288 | scriptcontext.doc.Views.Redraw()
1289 | return rc
1290 | for id in object_ids:
1291 | rhino_object = rhutil.coercerhinoobject(id, True, True)
1292 | rhino_object.Attributes.PlotWeightSource = Rhino.DocObjects.ObjectPlotWeightSource.PlotWeightFromObject
1293 | rhino_object.Attributes.PlotWeight = width
1294 | rhino_object.CommitChanges()
1295 | scriptcontext.doc.Views.Redraw()
1296 | return len(object_ids)
1297 |
1298 |
1299 | def ObjectPrintWidthSource(object_ids, source=None):
1300 | """Returns or modifies the print width source of an object
1301 | Parameters:
1302 | object_ids ([guid, ...]): identifiers of object(s)
1303 | source (number, optional): new print width source
1304 | 0 = print width by layer
1305 | 1 = print width by object
1306 | 3 = print width by parent
1307 | Returns:
1308 | number: If source is not specified, the object's current print width source
1309 | number: If source is specified, the object's previous print width source
1310 | number: If object_ids is a list or tuple, the number of objects modified
1311 | Example:
1312 | import rhinoscriptsyntax as rs
1313 | objects = rs.GetObjects("Select objects to reset print width source")
1314 | if objects:
1315 | for obj in objects: rs.ObjectPrintWidthSource(obj,0)
1316 | See Also:
1317 | ObjectPrintColor
1318 | """
1319 | id = rhutil.coerceguid(object_ids, False)
1320 | if id:
1321 | rhino_object = rhutil.coercerhinoobject(id, True, True)
1322 | rc = int(rhino_object.Attributes.PlotWeightSource)
1323 | if source is not None:
1324 | rhino_object.Attributes.PlotWeightSource = System.Enum.ToObject(Rhino.DocObjects.ObjectPlotWeightSource, source)
1325 | rhino_object.CommitChanges()
1326 | scriptcontext.doc.Views.Redraw()
1327 | return rc
1328 | for id in object_ids:
1329 | rhino_object = rhutil.coercerhinoobject(id, True, True)
1330 | rhino_object.Attributes.PlotWeightSource = System.Enum.ToObject(Rhino.DocObjects.ObjectPlotWeightSource, source)
1331 | rhino_object.CommitChanges()
1332 | scriptcontext.doc.Views.Redraw()
1333 | return len(object_ids)
1334 |
1335 |
1336 | def ObjectType(object_id):
1337 | """Returns the object type
1338 | Parameters:
1339 | object_id (guid): identifier of an object
1340 | Returns:
1341 | number: The object type if successful.
1342 | The valid object types are as follows:
1343 | Value Description
1344 | 0 Unknown object
1345 | 1 Point
1346 | 2 Point cloud
1347 | 4 Curve
1348 | 8 Surface or single-face brep
1349 | 16 Polysurface or multiple-face
1350 | 32 Mesh
1351 | 256 Light
1352 | 512 Annotation
1353 | 4096 Instance or block reference
1354 | 8192 Text dot object
1355 | 16384 Grip object
1356 | 32768 Detail
1357 | 65536 Hatch
1358 | 131072 Morph control
1359 | 262144 SubD
1360 | 134217728 Cage
1361 | 268435456 Phantom
1362 | 536870912 Clipping plane
1363 | 1073741824 Extrusion
1364 | Example:
1365 | import rhinoscriptsyntax as rs
1366 | obj = rs.GetObject("Select object")
1367 | if obj:
1368 | objtype = rs.ObjectType(obj)
1369 | print("Object type:{}".format(objtype))
1370 | See Also:
1371 | ObjectsByType
1372 | """
1373 | rhobj = rhutil.coercerhinoobject(object_id, True, True)
1374 | geom = rhobj.Geometry
1375 | if isinstance(geom, Rhino.Geometry.Brep) and geom.Faces.Count==1:
1376 | return 8 #surface
1377 | return int(geom.ObjectType)
1378 |
1379 |
1380 | def OrientObject(object_id, reference, target, flags=0):
1381 | """Orients a single object based on input points.
1382 |
1383 | If two 3-D points are specified, then this method will function similar to Rhino's Orient command. If more than two 3-D points are specified, then the function will orient similar to Rhino's Orient3Pt command.
1384 |
1385 | The orient flags values can be added together to specify multiple options.
1386 | Value Description
1387 | 1 Copy object. The default is not to copy the object.
1388 | 2 Scale object. The default is not to scale the object. Note, the scale option only applies if both reference and target contain only two 3-D points.
1389 |
1390 | Parameters:
1391 | object_id (guid): The identifier of an object
1392 | reference ([point, point, ...]): list of 3-D reference points.
1393 | target ([point, point, ...]): list of 3-D target points
1394 | flags (number): 1 = copy object
1395 | 2 = scale object
1396 | 3 = copy and scale
1397 | Returns:
1398 | guid: The identifier of the oriented object if successful.
1399 | Example:
1400 | import rhinoscriptsyntax as rs
1401 | obj = rs.GetObject("Select object to orient")
1402 | if obj:
1403 | reference = rs.GetPoints(message1="First reference point")
1404 | if reference and len(reference)>0:
1405 | target = rs.GetPoints(message1="First target point")
1406 | if target and len(target)>0:
1407 | rs.OrientObject( obj, reference, target )
1408 | See Also:
1409 |
1410 | """
1411 | object_id = rhutil.coerceguid(object_id, True)
1412 | from_array = rhutil.coerce3dpointlist(reference)
1413 | to_array = rhutil.coerce3dpointlist(target)
1414 | if from_array is None or to_array is None:
1415 | raise ValueError("Could not convert reference or target to point list")
1416 | from_count = len(from_array)
1417 | to_count = len(to_array)
1418 | if from_count<2 or to_count<2: raise Exception("point lists must have at least 2 values")
1419 |
1420 | copy = ((flags & 1) == 1)
1421 | scale = ((flags & 2) == 2)
1422 | xform_final = None
1423 | if from_count>2 and to_count>2:
1424 | #Orient3Pt
1425 | from_plane = Rhino.Geometry.Plane(from_array[0], from_array[1], from_array[2])
1426 | to_plane = Rhino.Geometry.Plane(to_array[0], to_array[1], to_array[2])
1427 | if not from_plane.IsValid or not to_plane.IsValid:
1428 | raise Exception("unable to create valid planes from point lists")
1429 | xform_final = Rhino.Geometry.Transform.PlaneToPlane(from_plane, to_plane)
1430 | else:
1431 | #Orient2Pt
1432 | xform_move = Rhino.Geometry.Transform.Translation( to_array[0]-from_array[0] )
1433 | xform_scale = Rhino.Geometry.Transform.Identity
1434 | v0 = from_array[1] - from_array[0]
1435 | v1 = to_array[1] - to_array[0]
1436 | if scale:
1437 | len0 = v0.Length
1438 | len1 = v1.Length
1439 | if len0<0.000001 or len1<0.000001: raise Exception("vector lengths too short")
1440 | scale = len1 / len0
1441 | if abs(1.0-scale)>=0.000001:
1442 | plane = Rhino.Geometry.Plane(from_array[0], v0)
1443 | xform_scale = Rhino.Geometry.Transform.Scale(plane, scale, scale, scale)
1444 | v0.Unitize()
1445 | v1.Unitize()
1446 | xform_rotate = Rhino.Geometry.Transform.Rotation(v0, v1, from_array[0])
1447 | xform_final = xform_move * xform_scale * xform_rotate
1448 | rc = scriptcontext.doc.Objects.Transform(object_id, xform_final, not copy)
1449 | if rc==System.Guid.Empty: return scriptcontext.errorhandler()
1450 | scriptcontext.doc.Views.Redraw()
1451 | return rc
1452 |
1453 |
1454 | def RotateObject(object_id, center_point, rotation_angle, axis=None, copy=False):
1455 | """Rotates a single object
1456 | Parameters:
1457 | object_id (guid): The identifier of an object to rotate
1458 | center_point (point): the center of rotation
1459 | rotation_angle (number): in degrees
1460 | axis (plane, optional): axis of rotation, If omitted, the Z axis of the active
1461 | construction plane is used as the rotation axis
1462 | copy (bool, optional): copy the object
1463 | Returns:
1464 | guid: Identifier of the rotated object if successful
1465 | None: on error
1466 | Example:
1467 | import rhinoscriptsyntax as rs
1468 | obj = rs.GetObject("Select object to rotate")
1469 | if obj:
1470 | point = rs.GetPoint("Center point of rotation")
1471 | if point: rs.RotateObject(obj, point, 45.0, None, copy=True)
1472 | See Also:
1473 | RotateObjects
1474 | """
1475 | rc = RotateObjects(object_id, center_point, rotation_angle, axis, copy)
1476 | if rc: return rc[0]
1477 | return scriptcontext.errorhandler()
1478 |
1479 |
1480 | def RotateObjects( object_ids, center_point, rotation_angle, axis=None, copy=False):
1481 | """Rotates multiple objects
1482 | Parameters:
1483 | object_ids ([guid, ...]): Identifiers of objects to rotate
1484 | center_point (point): the center of rotation
1485 | rotation_angle (number): in degrees
1486 | axis (plane, optional): axis of rotation, If omitted, the Z axis of the active
1487 | construction plane is used as the rotation axis
1488 | copy (bool, optional): copy the object
1489 | Returns:
1490 | list(guid, ...): identifiers of the rotated objects if successful
1491 | Example:
1492 | import rhinoscriptsyntax as rs
1493 | objs = rs.GetObjects("Select objects to rotate")
1494 | if objs:
1495 | point = rs.GetPoint("Center point of rotation")
1496 | if point:
1497 | rs.RotateObjects( objs, point, 45.0, None, True )
1498 | See Also:
1499 | RotateObject
1500 | """
1501 | center_point = rhutil.coerce3dpoint(center_point, True)
1502 | if not axis:
1503 | viewTable = scriptcontext.doc.Views
1504 | activeView = None
1505 | if viewTable is not None:
1506 | activeView = viewTable.ActiveView
1507 | if activeView is not None:
1508 | axis = scriptcontext.doc.Views.ActiveView.ActiveViewport.ConstructionPlane().Normal
1509 | else:
1510 | axis = Rhino.Geometry.Vector3d(0,0,1)
1511 | axis = rhutil.coerce3dvector(axis, True)
1512 | rotation_angle = Rhino.RhinoMath.ToRadians(rotation_angle)
1513 | xf = Rhino.Geometry.Transform.Rotation(rotation_angle, axis, center_point)
1514 | rc = TransformObjects(object_ids, xf, copy)
1515 | return rc
1516 |
1517 |
1518 | def ScaleObject(object_id, origin, scale, copy=False):
1519 | """Scales a single object. Can be used to perform a uniform or non-uniform
1520 | scale transformation. Scaling is based on the active construction plane.
1521 | Parameters:
1522 | object_id (guid): The identifier of an object
1523 | origin (point): the origin of the scale transformation
1524 | scale ([number, number, number]): three numbers that identify the X, Y, and Z axis scale factors to apply
1525 | copy (bool, optional): copy the object
1526 | Returns:
1527 | guid: Identifier of the scaled object if successful
1528 | None: on error
1529 | Example:
1530 | import rhinoscriptsyntax as rs
1531 | obj = rs.GetObject("Select object to scale")
1532 | if obj:
1533 | origin = rs.GetPoint("Origin point")
1534 | if origin:
1535 | rs.ScaleObject( obj, origin, (1,2,3), True )
1536 | See Also:
1537 | ScaleObjects
1538 | """
1539 | rc = ScaleObjects(object_id, origin, scale, copy )
1540 | if rc: return rc[0]
1541 | return scriptcontext.errorhandler()
1542 |
1543 |
1544 | def ScaleObjects(object_ids, origin, scale, copy=False):
1545 | """Scales one or more objects. Can be used to perform a uniform or non-
1546 | uniform scale transformation. Scaling is based on the active construction plane.
1547 | Parameters:
1548 | object_ids ([guid, ...]): Identifiers of objects to scale
1549 | origin (point): the origin of the scale transformation
1550 | scale ([number, number, number]): three numbers that identify the X, Y, and Z axis scale factors to apply
1551 | copy (bool, optional): copy the objects
1552 | Returns:
1553 | list(guid, ...): identifiers of the scaled objects if successful
1554 | None: on error
1555 | Example:
1556 | import rhinoscriptsyntax as rs
1557 | objs = rs.GetObjects("Select objects to scale")
1558 | if objs:
1559 | origin = rs.GetPoint("Origin point")
1560 | if origin:
1561 | rs.ScaleObjects( objs, origin, (2,2,2), True )
1562 | See Also:
1563 | ScaleObject
1564 | """
1565 | origin = rhutil.coerce3dpoint(origin, True)
1566 | scale = rhutil.coerce3dpoint(scale, True)
1567 | plane = scriptcontext.doc.Views.ActiveView.ActiveViewport.ConstructionPlane()
1568 | plane.Origin = origin
1569 | xf = Rhino.Geometry.Transform.Scale(plane, scale.X, scale.Y, scale.Z)
1570 | rc = TransformObjects(object_ids, xf, copy)
1571 | return rc
1572 |
1573 |
1574 | def SelectObject(object_id, redraw=True):
1575 | """Selects a single object
1576 | Parameters:
1577 | object_id (guid): the identifier of the object to select
1578 | Returns:
1579 | bool: True on success
1580 | Example:
1581 | import rhinoscriptsyntax as rs
1582 | rs.Command( "Line 0,0,0 5,5,0" )
1583 | id = rs.FirstObject()
1584 | if id: rs.SelectObject(id)
1585 | # Do something here...
1586 | rs.UnselectObject(id)
1587 | See Also:
1588 | IsObjectSelectable
1589 | IsObjectSelected
1590 | SelectObjects
1591 | UnselectObject
1592 | UnselectObjects
1593 | """
1594 | rhobj = rhutil.coercerhinoobject(object_id, True, True)
1595 | rhobj.Select(True)
1596 | if redraw: scriptcontext.doc.Views.Redraw()
1597 | return True
1598 |
1599 |
1600 | def SelectObjects( object_ids):
1601 | """Selects one or more objects
1602 | Parameters:
1603 | object_ids ([guid, ...]): identifiers of the objects to select
1604 | Returns:
1605 | number: number of selected objects
1606 | Example:
1607 | import rhinoscriptsyntax as rs
1608 | ids = rs.GetObjects("Select object to copy in-place")
1609 | if ids:
1610 | rs.UnselectObjects(ids)
1611 | copies = rs.CopyObjects(ids)
1612 | if copies: rs.SelectObjects(copies)
1613 | See Also:
1614 | IsObjectSelectable
1615 | IsObjectSelected
1616 | SelectObject
1617 | UnselectObject
1618 | UnselectObjects
1619 | """
1620 | id = rhutil.coerceguid(object_ids, False)
1621 | if id: object_ids = [id]
1622 | rc = 0
1623 | for id in object_ids:
1624 | if SelectObject(id, False)==True: rc += 1
1625 | if rc > 0: scriptcontext.doc.Views.Redraw()
1626 | return rc
1627 |
1628 |
1629 | def ShearObject(object_id, origin, reference_point, angle_degrees, copy=False):
1630 | """Perform a shear transformation on a single object
1631 | Parameters:
1632 | object_id (guid, ...): The identifier of an object
1633 | origin, reference_point (point) origin/reference point of the shear transformation
1634 | copy (bool, optional): copy the objects
1635 | Returns:
1636 | guid: Identifier of the sheared object if successful
1637 | None: on error
1638 | Example:
1639 | import rhinoscriptsyntax as rs
1640 | obj = rs.GetObject("Select object to shear")
1641 | if obj:
1642 | origin = rs.GetPoint("Origin point")
1643 | refpt = rs.GetPoint("Reference point")
1644 | if origin and refpt:
1645 | rs.ShearObject(obj, origin, refpt, 45.0, True)
1646 | See Also:
1647 | ShearObjects
1648 | """
1649 | rc = ShearObjects(object_id, origin, reference_point, angle_degrees, copy)
1650 | if rc: return rc[0]
1651 |
1652 |
1653 | def ShearObjects(object_ids, origin, reference_point, angle_degrees, copy=False):
1654 | """Shears one or more objects
1655 | Parameters:
1656 | object_ids ([guid, ...]): The identifiers objects to shear
1657 | origin, reference_point (point): origin/reference point of the shear transformation
1658 | copy (bool, optional): copy the objects
1659 | Returns:
1660 | list(guid, ...]): identifiers of the sheared objects if successful
1661 | Example:
1662 | import rhinoscriptsyntax as rs
1663 | object_ids = rs.GetObjects("Select objects to shear")
1664 | if object_ids:
1665 | origin = rs.GetPoint("Origin point")
1666 | refpt = rs.GetPoint("Reference point")
1667 | if origin and refpt:
1668 | rs.ShearObjects( object_ids, origin, refpt, 45.0, True )
1669 | See Also:
1670 | ShearObject
1671 | """
1672 | origin = rhutil.coerce3dpoint(origin, True)
1673 | reference_point = rhutil.coerce3dpoint(reference_point, True)
1674 | if (origin-reference_point).IsTiny(): return None
1675 | plane = scriptcontext.doc.Views.ActiveView.MainViewport.ConstructionPlane()
1676 | frame = Rhino.Geometry.Plane(plane)
1677 | frame.Origin = origin
1678 | frame.ZAxis = plane.Normal
1679 | yaxis = reference_point-origin
1680 | yaxis.Unitize()
1681 | frame.YAxis = yaxis
1682 | xaxis = Rhino.Geometry.Vector3d.CrossProduct(frame.ZAxis, frame.YAxis)
1683 | xaxis.Unitize()
1684 | frame.XAxis = xaxis
1685 |
1686 | world_plane = Rhino.Geometry.Plane.WorldXY
1687 | cob = Rhino.Geometry.Transform.ChangeBasis(world_plane, frame)
1688 | shear2d = Rhino.Geometry.Transform.Identity
1689 | shear2d[0,1] = math.tan(math.radians(angle_degrees))
1690 | cobinv = Rhino.Geometry.Transform.ChangeBasis(frame, world_plane)
1691 | xf = cobinv * shear2d * cob
1692 | rc = TransformObjects(object_ids, xf, copy)
1693 | return rc
1694 |
1695 |
1696 | def ShowObject(object_id):
1697 | """Shows a previously hidden object. Hidden objects are not visible, cannot
1698 | be snapped to and cannot be selected
1699 | Parameters:
1700 | object_id (guid): representing id of object to show
1701 | Returns:
1702 | bool: True of False indicating success or failure
1703 | Example:
1704 | import rhinoscriptsyntax as rs
1705 | obj = rs.GetObject("Select object to hide")
1706 | if obj: rs.HideObject(obj)
1707 | # Do something here...
1708 | rs.ShowObject( obj )
1709 | See Also:
1710 | HideObject
1711 | HideObjects
1712 | IsObjectHidden
1713 | ShowObjects
1714 | """
1715 | return ShowObjects(object_id)==1
1716 |
1717 |
1718 | def ShowObjects(object_ids):
1719 | """Shows one or more objects. Hidden objects are not visible, cannot be
1720 | snapped to and cannot be selected
1721 | Parameters:
1722 | object_ids ([guid, ...]): ids of objects to show
1723 | Returns:
1724 | number: Number of objects shown
1725 | Example:
1726 | import rhinoscriptsyntax as rs
1727 | objs = rs.GetObjects("Select objects to hide")
1728 | if objs: rs.HideObjects(objs)
1729 | #Do something here...
1730 | rs.ShowObjects( objs )
1731 | See Also:
1732 | HideObject
1733 | HideObjects
1734 | IsObjectHidden
1735 | ShowObject
1736 | """
1737 | id = rhutil.coerceguid(object_ids, False)
1738 | if id: object_ids = [id]
1739 | rc = 0
1740 | for id in object_ids:
1741 | id = rhutil.coerceguid(id, True)
1742 | if scriptcontext.doc.Objects.Show(id, False): rc += 1
1743 | if rc: scriptcontext.doc.Views.Redraw()
1744 | return rc
1745 |
1746 |
1747 | def TransformObject(object_id, matrix, copy=False):
1748 | """Moves, scales, or rotates an object given a 4x4 transformation matrix.
1749 | The matrix acts on the left.
1750 | Parameters:
1751 | object (guid): The identifier of the object.
1752 | matrix (transform): The transformation matrix (4x4 array of numbers).
1753 | copy (bool, optional): Copy the object.
1754 | Returns:
1755 | (guid): The identifier of the transformed object
1756 | None: if not successful, or on error
1757 | Example:
1758 | # Rotate an object by theta degrees about the world Z axis
1759 | import math
1760 | import rhinoscriptsyntax as rs
1761 | degrees = 90.0 # Some angle
1762 | radians = math.radians(degrees)
1763 | c = math.cos(radians)
1764 | s = math.sin(radians)
1765 | matrix = []
1766 | matrix.append( [c,-s, 0, 0] )
1767 | matrix.append( [s, c, 0, 0] )
1768 | matrix.append( [0, 0, 1, 0] )
1769 | matrix.append( [0, 0, 0, 1] )
1770 | obj = rs.GetObject("Select object to rotate")
1771 | if obj: rs.TransformObject( obj, matrix )
1772 | See Also:
1773 | TransformObjects
1774 | """
1775 | rc = TransformObjects(object_id, matrix, copy)
1776 | if rc: return rc[0]
1777 | return scriptcontext.errorhandler()
1778 |
1779 |
1780 | # this is also called by Copy, Scale, Mirror, Move, and Rotate functions defined above
1781 | def TransformObjects(object_ids, matrix, copy=False):
1782 | """Moves, scales, or rotates a list of objects given a 4x4 transformation
1783 | matrix. The matrix acts on the left.
1784 | Parameters:
1785 | object_ids [(guid, ...}): List of object identifiers.
1786 | matrix (transform): The transformation matrix (4x4 array of numbers).
1787 | copy (bool, optional): Copy the objects
1788 | Returns:
1789 | list(guid, ...): ids identifying the newly transformed objects
1790 | Example:
1791 | import rhinoscriptsyntax as rs
1792 | # Translate (move) objects by (10,10,0)
1793 | xform = rs.XformTranslation([10,10,0])
1794 | objs = rs.GetObjects("Select objects to translate")
1795 | if objs: rs.TransformObjects(objs, xform)
1796 | See Also:
1797 | TransformObject
1798 | """
1799 | xform = rhutil.coercexform(matrix, True)
1800 | id = rhutil.coerceguid(object_ids, False)
1801 | if id: object_ids = [id]
1802 | elif isinstance(object_ids, Rhino.Geometry.GeometryBase): object_ids = [object_ids]
1803 | elif type(object_ids) in __ALLOWED_TRANSFORM_TYPES: object_ids = [object_ids]
1804 | rc = []
1805 | for object_id in object_ids:
1806 | id = System.Guid.Empty
1807 | old_id = rhutil.coerceguid(object_id, False)
1808 | if old_id:
1809 | id = scriptcontext.doc.Objects.Transform(old_id, xform, not copy)
1810 | elif isinstance(object_id, Rhino.Geometry.GeometryBase):
1811 | if copy: object_id = object_id.Duplicate()
1812 | if not object_id.Transform(xform): raise Exception("Cannot apply transform to geometry.")
1813 | id = scriptcontext.doc.Objects.Add(object_id)
1814 | else:
1815 | type_of_id = type(object_id)
1816 | if type_of_id in __ALLOWED_TRANSFORM_TYPES:
1817 | if copy: object_id = System.ICloneable.Clone(object_id)
1818 | if object_id.Transform(xform) == False: #some of the Transform methods return bools, others have no return
1819 | raise Exception("Cannot apply transform to geometry.")
1820 | ot = scriptcontext.doc.Objects
1821 | fs = [ot.AddPoint,ot.AddLine,ot.AddRectangle,ot.AddCircle,ot.AddEllipse,ot.AddArc,ot.AddPolyline,ot.AddBox,ot.AddSphere]
1822 | t_index = __ALLOWED_TRANSFORM_TYPES.index(type_of_id)
1823 | id = fs[t_index](object_id)
1824 | else:
1825 | raise Exception("The {0} cannot be tranformed. A Guid or geometry types are expected.".format(type_of_id))
1826 | if id!=System.Guid.Empty: rc.append(id)
1827 | if rc: scriptcontext.doc.Views.Redraw()
1828 | return rc
1829 |
1830 |
1831 | def UnlockObject(object_id):
1832 | """Unlocks an object. Locked objects are visible, and can be snapped to,
1833 | but they cannot be selected.
1834 | Parameters:
1835 | object_id (guid): The identifier of an object
1836 | Returns:
1837 | bool: True or False indicating success or failure
1838 | Example:
1839 | import rhinoscriptsyntax as rs
1840 | obj = rs.GetObject("Select object to lock")
1841 | if obj: rs.LockObject(obj)
1842 | #Do something here...
1843 | rs.UnlockObject( obj )
1844 | See Also:
1845 | IsObjectLocked
1846 | LockObject
1847 | LockObjects
1848 | UnlockObjects
1849 | """
1850 | return UnlockObjects(object_id)==1
1851 |
1852 |
1853 | def UnlockObjects(object_ids):
1854 | """Unlocks one or more objects. Locked objects are visible, and can be
1855 | snapped to, but they cannot be selected.
1856 | Parameters:
1857 | object_ids ([guid, ...]): The identifiers of objects
1858 | Returns:
1859 | number: number of objects unlocked
1860 | Example:
1861 | import rhinoscriptsyntax as rs
1862 | objs = rs.GetObjects("Select objects to lock")
1863 | if objs: rs.LockObjects(objs)
1864 | #Do something here...
1865 | rs.UnlockObjects( objs )
1866 | See Also:
1867 | IsObjectLocked
1868 | LockObject
1869 | LockObjects
1870 | UnlockObject
1871 | """
1872 | id = rhutil.coerceguid(object_ids, False)
1873 | if id: object_ids = [id]
1874 | rc = 0
1875 | for id in object_ids:
1876 | id = rhutil.coerceguid(id, True)
1877 | if scriptcontext.doc.Objects.Unlock(id, False): rc += 1
1878 | if rc: scriptcontext.doc.Views.Redraw()
1879 | return rc
1880 |
1881 |
1882 | def UnselectObject(object_id):
1883 | """Unselects a single selected object
1884 | Parameters:
1885 | object_id: (guid): id of object to unselect
1886 | Returns:
1887 | bool: True of False indicating success or failure
1888 | Example:
1889 | import rhinoscriptsyntax as rs
1890 | rs.Command("Line 0,0,0 5,5,0")
1891 | obj = rs.FirstObject()
1892 | if obj: rs.SelectObject(obj)
1893 | #Do something here...
1894 | rs.UnselectObject( obj )
1895 | See Also:
1896 | IsObjectSelected
1897 | SelectObject
1898 | SelectObjects
1899 | UnselectObjects
1900 | """
1901 | return UnselectObjects(object_id)==1
1902 |
1903 |
1904 | def UnselectObjects(object_ids):
1905 | """Unselects one or more selected objects.
1906 | Parameters:
1907 | object_ids ([guid, ...]): identifiers of the objects to unselect.
1908 | Returns:
1909 | number: The number of objects unselected
1910 | Example:
1911 | import rhinoscriptsyntax as rs
1912 | objects = rs.GetObjects("Select object to copy in-place")
1913 | if objects:
1914 | rs.UnselectObjects(objects)
1915 | copies= rs.CopyObjects(objects)
1916 | if copies: rs.SelectObjects(copies)
1917 | See Also:
1918 | IsObjectSelected
1919 | SelectObject
1920 | SelectObjects
1921 | UnselectObject
1922 | """
1923 | id = rhutil.coerceguid(object_ids, False)
1924 | if id: object_ids = [id]
1925 | count = len(object_ids)
1926 | for id in object_ids:
1927 | obj = rhutil.coercerhinoobject(id, True, True)
1928 | obj.Select(False)
1929 | if count: scriptcontext.doc.Views.Redraw()
1930 | return count
1931 |
```