import rhino3dm
from dataclasses import dataclass, field
import random
import string
print('rhino3dm ver: ', rhino3dm.__version__)
def generate_id() -> str:
return "".join(random.choices(string.ascii_lowercase, k=12))
rhino3dm ver: 8.9.0
.3dm
filenotes
Layer
ObjectsTable
OatPV
pv
cable
text
MyPt
pt
TextDot
EarthAnchorPoint
.3dm
filenotes
file3dm_notes = rhino3dm.File3dm.ReadNotes(rhino_f_path).split(';')[:-1]
for idx, note in enumerate(file3dm_notes):
print(
note.strip().split(':')[0].strip(),
' : ',
note.strip().split(':')[1].strip()
)
name : nongke
lati : 20
long : 100
method rhino3dm.File3dm.ReadNotes()
Reads only the notes from an existing 3dm file.
Parameters:
Returns:
Return type:
Layer
layer_table = file3dm_obj.Layers
print('layer table object: ', layer_table)
print('num of layers: ', len(layer_table))
print()
for idx, layer in enumerate(iter(layer_table)):
print('layer ', idx, ' Object : ', layer)
print(' ', idx, ' Id : ', layer.Id)
print(' ', idx, ' Index : ', layer.Index)
print(' ', idx, ' Name : ', layer.Name)
print(' ', idx, ' Fullpath: ', layer.FullPath)
print(' ', idx, ' Color : ', layer.Color)
print()
layer table object: <rhino3dm._rhino3dm.File3dmLayerTable object at 0x119a22cf0>
num of layers: 6
layer 0 Object : <rhino3dm._rhino3dm.Layer object at 0x119a463f0>
0 Id : 212960d6-ad46-439d-8516-772ce85fde03
0 Index : 0
0 Name : 0
0 Fullpath: 0
0 Color : (0, 0, 0, 255)
layer 1 Object : <rhino3dm._rhino3dm.Layer object at 0x119a6a030>
1 Id : 25488de8-55a5-470b-a098-ea3c8c1220b3
1 Index : 1
1 Name : ! cable
1 Fullpath: ! cable
1 Color : (255, 191, 0, 255)
layer 2 Object : <rhino3dm._rhino3dm.Layer object at 0x119a47f30>
2 Id : 6f40c8fc-3378-4efb-a590-0af0bf5a85dc
2 Index : 2
2 Name : ! num
2 Fullpath: ! num
2 Color : (0, 127, 0, 255)
layer 3 Object : <rhino3dm._rhino3dm.Layer object at 0x119a44e30>
3 Id : 4af54445-51c1-4fff-b520-54055554debc
3 Index : 3
3 Name : ! num_TextDot
3 Fullpath: ! num_TextDot
3 Color : (0, 127, 0, 255)
layer 4 Object : <rhino3dm._rhino3dm.Layer object at 0x119a9c530>
4 Id : 0428f404-44e4-47e7-ae60-72284d0a89f7
4 Index : 4
4 Name : ! pv
4 Fullpath: ! pv
4 Color : (0, 0, 0, 255)
layer 5 Object : <rhino3dm._rhino3dm.Layer object at 0x119a9cd30>
5 Id : c0c0bbb7-2adb-4301-8c42-c09459068132
5 Index : 5
5 Name : pv_surf
5 Fullpath: pv_surf
5 Color : (191, 191, 255, 255)
ObjectsTable
OatPV
pv
layer_idx_of_pv = 4
cnt_pv = 0
max_num_obj = 3
total_num_pv = 0
oatPV_list = []
for idx, obj in enumerate(iter(object_table)):
if (obj.Attributes.LayerIndex == layer_idx_of_pv) and (cnt_pv < max_num_obj):
print('object ', idx, ' Object : ', obj)
print(' ', idx, ' Geometry : ', obj.Geometry)
print(' ', idx, ' Attributes : ', obj.Attributes)
print(' ', idx, ' Attributes.Id : ', obj.Attributes.Id)
print(' ', idx, ' Attributes.Name : ', obj.Attributes.Name)
print(' ', idx, ' Attributes.LayerIndex : ', obj.Attributes.LayerIndex)
print(' ', idx, ' Attributes.ObjectColor: ', obj.Attributes.ObjectColor)
print(' ', idx, ' Attributes.Visible : ', obj.Attributes.Visible)
print()
# if geom is PolylineCurve, print its details
if isinstance(obj.Geometry, rhino3dm.PolylineCurve):
print(' curve IsClosed :', obj.Geometry.IsClosed)
print(' curve IsPlanar :', obj.Geometry.IsPlanar())
print(' curve IsPolyline :', obj.Geometry.IsPolyline())
print()
print(' curve PointCount :', obj.Geometry.PointCount)
for i in range(obj.Geometry.PointCount):
print(' curve pt ', i, ' : ', obj.Geometry.Point(i))
# print(' curve pt ', i, ' : ', obj.Geometry.ToPolyline().PointAt(i)) -- alternative
print()
print(' curve SegmentCount :', obj.Geometry.ToPolyline().SegmentCount)
for i in range(obj.Geometry.ToPolyline().SegmentCount):
print(' segement ', i, ' : ', obj.Geometry.ToPolyline().SegmentAt(i))
print()
oatPV = OatPV(idx, obj.Geometry)
oatPV_list.append(oatPV)
cnt_pv += 1
else:
pass
for idx, obj in enumerate(iter(object_table)):
if (obj.Attributes.LayerIndex == layer_idx_of_pv):
total_num_pv += 1
print('total num of pv: ', total_num_pv)
print()
for idx, oatPV in enumerate(oatPV_list):
print('oatPV ', idx, ' : ', oatPV)
object 72 Object : <rhino3dm._rhino3dm.File3dmObject object at 0x119a8cc70>
72 Geometry : <rhino3dm._rhino3dm.PolylineCurve object at 0x119a751f0>
72 Attributes : <rhino3dm._rhino3dm.ObjectAttributes object at 0x119a751f0>
72 Attributes.Id : 36f08c55-5986-4aa2-b0b8-ae56b26b043c
72 Attributes.Name :
72 Attributes.LayerIndex : 4
72 Attributes.ObjectColor: (0, 0, 0, 255)
72 Attributes.Visible : True
curve IsClosed : True
curve IsPlanar : True
curve IsPolyline : True
curve PointCount : 5
curve pt 0 : 35.10614556790845,209.9589626310085,0.0
curve pt 1 : 35.28014104193071,210.94573999952675,0.0
curve pt 2 : 36.95467106254545,210.65047517688606,0.0
curve pt 3 : 36.78067558852319,209.6636978083678,0.0
curve pt 4 : 35.10614556790845,209.9589626310085,0.0
curve SegmentCount : 4
segement 0 : <rhino3dm._rhino3dm.LineCurve object at 0x119a2d7b0>
segement 1 : <rhino3dm._rhino3dm.LineCurve object at 0x119a2d7b0>
segement 2 : <rhino3dm._rhino3dm.LineCurve object at 0x119a2d7b0>
segement 3 : <rhino3dm._rhino3dm.LineCurve object at 0x119a2d7b0>
object 73 Object : <rhino3dm._rhino3dm.File3dmObject object at 0x119a693f0>
73 Geometry : <rhino3dm._rhino3dm.PolylineCurve object at 0x1093303b0>
73 Attributes : <rhino3dm._rhino3dm.ObjectAttributes object at 0x1093303b0>
73 Attributes.Id : 2a5e0e48-e0ca-4d27-896e-1d3f2435645e
73 Attributes.Name :
73 Attributes.LayerIndex : 4
73 Attributes.ObjectColor: (0, 0, 0, 255)
73 Attributes.Visible : True
curve IsClosed : True
curve IsPlanar : True
curve IsPolyline : True
curve PointCount : 5
curve pt 0 : 99.74743231227566,208.92053051678556,0.0
curve pt 1 : 99.92142776982108,209.90730779185895,0.0
curve pt 2 : 101.59595209030033,209.61204397430595,0.0
curve pt 3 : 101.4219566327549,208.62526669923255,0.0
curve pt 4 : 99.74743231227566,208.92053051678556,0.0
curve SegmentCount : 4
segement 0 : <rhino3dm._rhino3dm.LineCurve object at 0x119ab02f0>
segement 1 : <rhino3dm._rhino3dm.LineCurve object at 0x119ab02f0>
segement 2 : <rhino3dm._rhino3dm.LineCurve object at 0x119ab02f0>
segement 3 : <rhino3dm._rhino3dm.LineCurve object at 0x119ab02f0>
object 74 Object : <rhino3dm._rhino3dm.File3dmObject object at 0x119ada8f0>
74 Geometry : <rhino3dm._rhino3dm.PolylineCurve object at 0x119ada8b0>
74 Attributes : <rhino3dm._rhino3dm.ObjectAttributes object at 0x119ada8b0>
74 Attributes.Id : 56851944-a49b-49bc-af4f-44c779262974
74 Attributes.Name :
74 Attributes.LayerIndex : 4
74 Attributes.ObjectColor: (0, 0, 0, 255)
74 Attributes.Visible : True
curve IsClosed : True
curve IsPlanar : True
curve IsPolyline : True
curve PointCount : 5
curve pt 0 : 91.36944042680591,198.12663899455657,0.0
curve pt 1 : 91.54343588435134,199.11341626962997,0.0
curve pt 2 : 93.21797160510158,198.81815044190162,0.0
curve pt 3 : 93.04397614755615,197.83137316682823,0.0
curve pt 4 : 91.36944042680591,198.12663899455657,0.0
curve SegmentCount : 4
segement 0 : <rhino3dm._rhino3dm.LineCurve object at 0x119adad30>
segement 1 : <rhino3dm._rhino3dm.LineCurve object at 0x119adad30>
segement 2 : <rhino3dm._rhino3dm.LineCurve object at 0x119adad30>
segement 3 : <rhino3dm._rhino3dm.LineCurve object at 0x119adad30>
total num of pv: 2400
oatPV 0 : OatPV(idx=72, outline=<rhino3dm._rhino3dm.PolylineCurve object at 0x119a2d7b0>, str_num='', id='amzmjffvbetm')
oatPV 1 : OatPV(idx=73, outline=<rhino3dm._rhino3dm.PolylineCurve object at 0x119ab02f0>, str_num='', id='oqkfmiycadvk')
oatPV 2 : OatPV(idx=74, outline=<rhino3dm._rhino3dm.PolylineCurve object at 0x119adad30>, str_num='', id='zcrcwsjlmwye')
cable
layer_idx_of_cable = 1
cnt_cable = 0
max_num_obj = 3
total_num_cable = 0
for idx, obj in enumerate(iter(object_table)):
# print the first 3 obj on a layer
if (obj.Attributes.LayerIndex == layer_idx_of_cable) and (cnt_cable < max_num_obj):
print('object ', idx, ' Object : ', obj)
print(' ', idx, ' Geometry : ', obj.Geometry)
print(' ', idx, ' Attributes : ', obj.Attributes)
print(' ', idx, ' Attributes.Id : ', obj.Attributes.Id)
print(' ', idx, ' Attributes.Name : ', obj.Attributes.Name)
print(' ', idx, ' Attributes.LayerIndex : ', obj.Attributes.LayerIndex)
print(' ', idx, ' Attributes.ObjectColor: ', obj.Attributes.ObjectColor)
print(' ', idx, ' Attributes.Visible : ', obj.Attributes.Visible)
print()
# if geom is PolylineCurve, print its details
if isinstance(obj.Geometry, rhino3dm.PolylineCurve):
print(' curve IsClosed :', obj.Geometry.IsClosed)
print(' curve IsPlanar :', obj.Geometry.IsPlanar())
print(' curve IsPolyline :', obj.Geometry.IsPolyline())
print()
print(' curve PointCount :', obj.Geometry.PointCount)
for i in range(obj.Geometry.PointCount):
print(' curve pt ', i, ' : ', obj.Geometry.Point(i))
# print(' curve pt ', i, ' : ', obj.Geometry.ToPolyline().PointAt(i)) -- alternative
print()
print(' curve SegmentCount :', obj.Geometry.ToPolyline().SegmentCount)
for i in range(obj.Geometry.ToPolyline().SegmentCount):
print(' segement ', i, ' : ', obj.Geometry.ToPolyline().SegmentAt(i))
print()
cnt_cable += 1
else:
pass
for idx, obj in enumerate(iter(object_table)):
if (obj.Attributes.LayerIndex == layer_idx_of_cable):
total_num_cable += 1
print('total num of cable: ', total_num_cable)
object 0 Object : <rhino3dm._rhino3dm.File3dmObject object at 0x1093300b0>
0 Geometry : <rhino3dm._rhino3dm.PolylineCurve object at 0x119a441b0>
0 Attributes : <rhino3dm._rhino3dm.ObjectAttributes object at 0x119a441b0>
0 Attributes.Id : fa8bcce9-953e-47f0-bcfc-4de7a87e0c76
0 Attributes.Name :
0 Attributes.LayerIndex : 1
0 Attributes.ObjectColor: (0, 0, 0, 255)
0 Attributes.Visible : True
curve IsClosed : False
curve IsPlanar : True
curve IsPolyline : True
curve PointCount : 6
curve pt 0 : 14.298569362095407,251.0734261769187,0.0
curve pt 1 : 2.232971074135598,253.20091370662107,0.0
curve pt 2 : 1.9377067622771804,251.5263865827965,0.0
curve pt 3 : 14.003304539454732,249.398896156304,0.0
curve pt 4 : 13.700564187398314,247.72568133454058,0.0
curve pt 5 : 8.684444947408636,248.6101584950016,0.0
curve SegmentCount : 5
segement 0 : <rhino3dm._rhino3dm.LineCurve object at 0x119a441b0>
segement 1 : <rhino3dm._rhino3dm.LineCurve object at 0x119a441b0>
segement 2 : <rhino3dm._rhino3dm.LineCurve object at 0x119a441b0>
segement 3 : <rhino3dm._rhino3dm.LineCurve object at 0x119a441b0>
segement 4 : <rhino3dm._rhino3dm.LineCurve object at 0x119a441b0>
object 1 Object : <rhino3dm._rhino3dm.File3dmObject object at 0x109332870>
1 Geometry : <rhino3dm._rhino3dm.PolylineCurve object at 0x1093300b0>
1 Attributes : <rhino3dm._rhino3dm.ObjectAttributes object at 0x1093300b0>
1 Attributes.Id : a11caa23-6a3a-4dac-95b5-1c6005646eae
1 Attributes.Name :
1 Attributes.LayerIndex : 1
1 Attributes.ObjectColor: (0, 0, 0, 255)
1 Attributes.Visible : True
curve IsClosed : False
curve IsPlanar : True
curve IsPolyline : True
curve PointCount : 6
curve pt 0 : 7.675239846498812,248.7881056446808,0.0
curve pt 1 : 1.6424414370926925,249.851853712114,0.0
curve pt 2 : 1.3471761119082686,248.1773208414315,0.0
curve pt 3 : 13.412773889085692,246.04983041493904,0.0
curve pt 4 : 12.395163638063877,240.27867589886242,0.0
curve pt 5 : 1.3387704125292785,242.22821633384646,0.0
curve SegmentCount : 5
segement 0 : <rhino3dm._rhino3dm.LineCurve object at 0x119a3a270>
segement 1 : <rhino3dm._rhino3dm.LineCurve object at 0x119a3a270>
segement 2 : <rhino3dm._rhino3dm.LineCurve object at 0x119a3a270>
segement 3 : <rhino3dm._rhino3dm.LineCurve object at 0x119a3a270>
segement 4 : <rhino3dm._rhino3dm.LineCurve object at 0x119a3a270>
object 2 Object : <rhino3dm._rhino3dm.File3dmObject object at 0x119ada270>
2 Geometry : <rhino3dm._rhino3dm.PolylineCurve object at 0x109332870>
2 Attributes : <rhino3dm._rhino3dm.ObjectAttributes object at 0x109332870>
2 Attributes.Id : f2f27645-020b-4020-ab4f-369b6b5b3a71
2 Attributes.Name :
2 Attributes.LayerIndex : 1
2 Attributes.ObjectColor: (0, 0, 0, 255)
2 Attributes.Visible : True
curve IsClosed : False
curve IsPlanar : True
curve IsPolyline : True
curve PointCount : 7
curve pt 0 : 0.32956586088634054,242.40616632535492,0.0
curve pt 1 : 0.04480460092740657,240.7451976669099,0.0
curve pt 2 : 12.101217243309726,238.611623054351,0.0
curve pt 3 : 11.80726985994474,236.94456460314893,0.0
curve pt 4 : -0.2596458508139179,239.06458065688344,0.0
curve pt 5 : -0.6036829981151328,237.11344903812096,0.0
curve pt 6 : 3.4155472784859384,236.38933677866157,0.0
curve SegmentCount : 6
segement 0 : <rhino3dm._rhino3dm.LineCurve object at 0x109332870>
segement 1 : <rhino3dm._rhino3dm.LineCurve object at 0x109332870>
segement 2 : <rhino3dm._rhino3dm.LineCurve object at 0x109332870>
segement 3 : <rhino3dm._rhino3dm.LineCurve object at 0x109332870>
segement 4 : <rhino3dm._rhino3dm.LineCurve object at 0x109332870>
segement 5 : <rhino3dm._rhino3dm.LineCurve object at 0x109332870>
total num of cable: 72
text
layer_idx_of_text = 2
cnt_text = 0
max_num_obj = 3
total_num_text = 0
for idx, obj in enumerate(iter(object_table)):
if (obj.Attributes.LayerIndex == layer_idx_of_text) and (cnt_text < max_num_obj):
print('object ', idx, ' Object : ', obj)
print(' ', idx, ' Geometry : ', obj.Geometry)
print(' ', idx, ' Attributes : ', obj.Attributes)
print(' ', idx, ' Attributes.Id : ', obj.Attributes.Id)
print(' ', idx, ' Attributes.Name : ', obj.Attributes.Name)
print(' ', idx, ' Attributes.LayerIndex : ', obj.Attributes.LayerIndex)
print(' ', idx, ' Attributes.ObjectColor : ', obj.Attributes.ObjectColor)
print(' ', idx, ' Attributes.Visible : ', obj.Attributes.Visible)
print()
# if geom is AnnotationBase, print its PlainText
if isinstance(obj.Geometry, rhino3dm.AnnotationBase):
print(' Geometry.PlainText : ', obj.Geometry.PlainText)
print()
cnt_text += 1
for idx, obj in enumerate(iter(object_table)):
if (obj.Attributes.LayerIndex == layer_idx_of_text):
total_num_text += 1
print('total num of text: ', total_num_text)
object 4944 Object : <rhino3dm._rhino3dm.File3dmObject object at 0x109323bb0>
4944 Geometry : <rhino3dm._rhino3dm.Text object at 0x119ab21b0>
4944 Attributes : <rhino3dm._rhino3dm.ObjectAttributes object at 0x119ab21b0>
4944 Attributes.Id : c9b6923f-bb4c-4d0c-8407-9c230a7f9d5c
4944 Attributes.Name :
4944 Attributes.LayerIndex : 2
4944 Attributes.ObjectColor : (0, 0, 0, 255)
4944 Attributes.Visible : True
Geometry.PlainText : 6-6-1
object 4945 Object : <rhino3dm._rhino3dm.File3dmObject object at 0x119a441b0>
4945 Geometry : <rhino3dm._rhino3dm.Text object at 0x109323bb0>
4945 Attributes : <rhino3dm._rhino3dm.ObjectAttributes object at 0x109323bb0>
4945 Attributes.Id : cef51aca-20e2-495b-97e2-6e0e1c5b8494
4945 Attributes.Name :
4945 Attributes.LayerIndex : 2
4945 Attributes.ObjectColor : (0, 0, 0, 255)
4945 Attributes.Visible : True
Geometry.PlainText : 1-1-1
object 4946 Object : <rhino3dm._rhino3dm.File3dmObject object at 0x119a462b0>
4946 Geometry : <rhino3dm._rhino3dm.Text object at 0x119a441b0>
4946 Attributes : <rhino3dm._rhino3dm.ObjectAttributes object at 0x119a441b0>
4946 Attributes.Id : 09230350-7cef-453f-b45a-bf53ea14eb44
4946 Attributes.Name :
4946 Attributes.LayerIndex : 2
4946 Attributes.ObjectColor : (0, 0, 0, 255)
4946 Attributes.Visible : True
Geometry.PlainText : 1-1-2
total num of text: 72
MyPt
pt
# layer_idx_of_pt = 2
# cnt_pt = 0
# max_num_obj = 3
# total_num_pt = 0
#
# oaPt_list_1 = []
#
# for idx, obj in enumerate(iter(object_table)):
# if (obj.Attributes.LayerIndex == layer_idx_of_pt) and (cnt_pt < max_num_obj):
# print('object ', idx, ' Object : ', obj)
# print(' ', idx, ' Geometry : ', obj.Geometry)
# print(' ', idx, ' Attributes : ', obj.Attributes)
# print(' ', idx, ' Attributes.Id : ', obj.Attributes.Id)
# print(' ', idx, ' Attributes.Name : ', obj.Attributes.Name)
# print(' ', idx, ' Attributes.LayerIndex : ', obj.Attributes.LayerIndex)
# print(' ', idx, ' Attributes.ObjectColor : ', obj.Attributes.ObjectColor)
# print(' ', idx, ' Attributes.Visible : ', obj.Attributes.Visible)
# print()
#
# # if geom is Point, print its xyz
# if isinstance(obj.Geometry, rhino3dm.Point):
# pt = obj.Geometry.Location
# print(' Geometry.Location.X : ', pt.X)
# print(' Geometry.Location.Y : ', pt.Y)
# print(' Geometry.Location.Z : ', pt.Z)
#
# oatPt = OatPt(idx, pt, str(idx))
# oaPt_list_1.append(oatPt)
#
# print()
#
# for idx, obj in enumerate(iter(object_table)):
# if (obj.Attributes.LayerIndex == layer_idx_of_pt):
# total_num_pt += 1
#
# print('total num of pt: ', total_num_pt)
TextDot
max_num_obj = 3
cnt_textDot = 0
total_num_textDot = 0
for idx, obj in enumerate(iter(object_table)):
obj_geom = obj.Geometry
obj_attr = obj.Attributes
if (isinstance(obj_geom, rhino3dm.TextDot)) and (cnt_textDot < max_num_obj):
print('textDot object ', idx, ' : ', obj_geom)
print(' position : ', obj_geom.Point)
print(' text : ', obj_geom.Text)
print(' Attributes object : ', obj_attr)
print(' attribs.Id : ', obj_attr.Id)
print(' attribs.Name : ', obj_attr.Name)
print(' attribs.LayerIndex : ', obj_attr.LayerIndex)
print(' attribs.ObjectColor : ', obj_attr.ObjectColor)
print(' attribs.Visible : ', obj_attr.Visible)
print()
cnt_textDot += 1
for idx, obj in enumerate(iter(object_table)):
if isinstance(obj.Geometry, rhino3dm.TextDot):
total_num_textDot += 1
print('total num of textDot: ', total_num_textDot)
textDot object 4872 : <rhino3dm._rhino3dm.TextDot object at 0x109316830>
position : 27.064173777436537,200.67848337106952,0.0
text : 6-6-1
Attributes object : <rhino3dm._rhino3dm.ObjectAttributes object at 0x109332430>
attribs.Id : ace09291-2363-4c79-a99d-2ae7be61531d
attribs.Name :
attribs.LayerIndex : 3
attribs.ObjectColor : (0, 0, 0, 255)
attribs.Visible : True
textDot object 4873 : <rhino3dm._rhino3dm.TextDot object at 0x1093598f0>
position : 35.21171531476992,205.36180830113673,0.0
text : 1-1-1
Attributes object : <rhino3dm._rhino3dm.ObjectAttributes object at 0x109316830>
attribs.Id : b7bc5562-82b9-4119-b8ef-aa9c68ce3dee
attribs.Name :
attribs.LayerIndex : 3
attribs.ObjectColor : (0, 0, 0, 255)
attribs.Visible : True
textDot object 4874 : <rhino3dm._rhino3dm.TextDot object at 0x119a3b0f0>
position : 42.1912202855743,199.72217247117237,0.0
text : 1-1-2
Attributes object : <rhino3dm._rhino3dm.ObjectAttributes object at 0x1093598f0>
attribs.Id : 65b8075f-ceac-48f6-9b96-6d021fe2ad2a
attribs.Name :
attribs.LayerIndex : 3
attribs.ObjectColor : (0, 0, 0, 255)
attribs.Visible : True
total num of textDot: 72
EarthAnchorPoint
# get the `Settings` object as a property of File3dm object
file3dm_settings = file3dm_obj.Settings
print('file3dm settings: ', file3dm_settings)
# get `EarthAnchorPoint` in settings
earthAnchorPoint = file3dm_settings.EarthAnchorPoint
print('EarthAnchorPoint: ', earthAnchorPoint)
print()
# check if the earthAnchorPoint is set or not
earthAnchorPoint_set_or_not = earthAnchorPoint.EarthLocationIsSet()
print('earthAnchorPoint is set: ', earthAnchorPoint_set_or_not)
# get lat and lon of earthAnchorPoint
earthAnchorPoint_lat = earthAnchorPoint.EarthBasepointLatitude
earthAnchorPoint_lon = earthAnchorPoint.EarthBasepointLongitude
print('earthAnchorPoint latitude : ', earthAnchorPoint_lat)
print('earthAnchorPoint longitude: ', earthAnchorPoint_lon)
print()
# get ModelBasePoint
modelBasePoint = earthAnchorPoint.ModelBasePoint
print('model base point: ', modelBasePoint)
# get `ModelToEarthTransform`
modelUnitSystem = file3dm_settings.ModelUnitSystem
print('modelUnitSystem : ', modelUnitSystem)
modelToEarthTransform = earthAnchorPoint.GetModelToEarthTransform(modelUnitSystem)
print('modelToEarthTransform : ', modelToEarthTransform)
# create a test pt
# test_pt = rhino3dm.Point3d(91.2724, 187.784, 0)
test_pt = rhino3dm.Point3d(140.492,236.638,0)
test_pt_transformed_in_lon_lat_ele = test_pt.Transform(modelToEarthTransform)
print('test pt transformed in lon, lat, ele: ', test_pt_transformed_in_lon_lat_ele)
file3dm settings: <rhino3dm._rhino3dm.File3dmSettings object at 0x119a2c9f0>
EarthAnchorPoint: <rhino3dm._rhino3dm.EarthAnchorPoint object at 0x109322670>
earthAnchorPoint is set: True
earthAnchorPoint latitude : 22.716611
earthAnchorPoint longitude: 120.534701
model base point: 1.887214762712837,254.12517650890035,0.0
modelUnitSystem : UnitSystem.Meters
modelToEarthTransform : <rhino3dm._rhino3dm.Transform object at 0x109323330>
test pt transformed in lon, lat, ele: 120.53605149715442,22.716453831047513,0.0
property rhino3dm.File3dm.Settings
File3dmSettings: Settings include tolerance, and unit system, and defaults used for creating views and objects.
Return: A rhino3dm.File3dm.Settings object
class rhino3dm.EarthAnchorPoint
Property:
EarthBasepointLatitude
EarthBasepointLongitude
ModelBasePoint
GetModelToEarthTransform(modelUnitSystem)
---
title: "rhino3dm exercise 02"
filters:
- line-highlight
---
## import and util funcs
```{python}
import rhino3dm
from dataclasses import dataclass, field
import random
import string
print('rhino3dm ver: ', rhino3dm.__version__)
def generate_id() -> str:
return "".join(random.choices(string.ascii_lowercase, k=12))
```
## read `.3dm` file
```{python}
rhino_f_path = "./data/nongke.3dm"
file3dm_obj = rhino3dm.File3dm.Read(rhino_f_path)
print('rhino3dm.File3dm object: ', file3dm_obj)
```
## read `notes`
```{python}
file3dm_notes = rhino3dm.File3dm.ReadNotes(rhino_f_path).split(';')[:-1]
for idx, note in enumerate(file3dm_notes):
print(
note.strip().split(':')[0].strip(),
' : ',
note.strip().split(':')[1].strip()
)
```
:::: {.column-margin}
::: {.callout-tip title="File3dm.ReadNotes()" collapse="true"}
[**_method_** `rhino3dm.File3dm.ReadNotes()`](https://mcneel.github.io/rhino3dm/python/api/File3dm.html#rhino3dm.File3dm.ReadNotes)
<br />
Reads only the notes from an existing 3dm file.
<br />
Parameters:
- path (str) – The file from which to read the notes.
Returns:
- The 3dm file notes.
Return type:
- str
:::
::::
## read `Layer`
```{python}
layer_table = file3dm_obj.Layers
print('layer table object: ', layer_table)
print('num of layers: ', len(layer_table))
print()
for idx, layer in enumerate(iter(layer_table)):
print('layer ', idx, ' Object : ', layer)
print(' ', idx, ' Id : ', layer.Id)
print(' ', idx, ' Index : ', layer.Index)
print(' ', idx, ' Name : ', layer.Name)
print(' ', idx, ' Fullpath: ', layer.FullPath)
print(' ', idx, ' Color : ', layer.Color)
print()
```
## read `ObjectsTable`
```{python}
object_table = file3dm_obj.Objects
print('object table: ', object_table, '\n')
```
## create dataclass `OatPV`
```{python}
@dataclass
class OatPV:
idx: int
outline: rhino3dm.PolylineCurve
str_num: str = ''
id: str = field(default_factory=generate_id)
```
## read `pv`
```{python}
layer_idx_of_pv = 4
cnt_pv = 0
max_num_obj = 3
total_num_pv = 0
oatPV_list = []
for idx, obj in enumerate(iter(object_table)):
if (obj.Attributes.LayerIndex == layer_idx_of_pv) and (cnt_pv < max_num_obj):
print('object ', idx, ' Object : ', obj)
print(' ', idx, ' Geometry : ', obj.Geometry)
print(' ', idx, ' Attributes : ', obj.Attributes)
print(' ', idx, ' Attributes.Id : ', obj.Attributes.Id)
print(' ', idx, ' Attributes.Name : ', obj.Attributes.Name)
print(' ', idx, ' Attributes.LayerIndex : ', obj.Attributes.LayerIndex)
print(' ', idx, ' Attributes.ObjectColor: ', obj.Attributes.ObjectColor)
print(' ', idx, ' Attributes.Visible : ', obj.Attributes.Visible)
print()
# if geom is PolylineCurve, print its details
if isinstance(obj.Geometry, rhino3dm.PolylineCurve):
print(' curve IsClosed :', obj.Geometry.IsClosed)
print(' curve IsPlanar :', obj.Geometry.IsPlanar())
print(' curve IsPolyline :', obj.Geometry.IsPolyline())
print()
print(' curve PointCount :', obj.Geometry.PointCount)
for i in range(obj.Geometry.PointCount):
print(' curve pt ', i, ' : ', obj.Geometry.Point(i))
# print(' curve pt ', i, ' : ', obj.Geometry.ToPolyline().PointAt(i)) -- alternative
print()
print(' curve SegmentCount :', obj.Geometry.ToPolyline().SegmentCount)
for i in range(obj.Geometry.ToPolyline().SegmentCount):
print(' segement ', i, ' : ', obj.Geometry.ToPolyline().SegmentAt(i))
print()
oatPV = OatPV(idx, obj.Geometry)
oatPV_list.append(oatPV)
cnt_pv += 1
else:
pass
for idx, obj in enumerate(iter(object_table)):
if (obj.Attributes.LayerIndex == layer_idx_of_pv):
total_num_pv += 1
print('total num of pv: ', total_num_pv)
print()
for idx, oatPV in enumerate(oatPV_list):
print('oatPV ', idx, ' : ', oatPV)
```
## read `cable`
```{python}
layer_idx_of_cable = 1
cnt_cable = 0
max_num_obj = 3
total_num_cable = 0
for idx, obj in enumerate(iter(object_table)):
# print the first 3 obj on a layer
if (obj.Attributes.LayerIndex == layer_idx_of_cable) and (cnt_cable < max_num_obj):
print('object ', idx, ' Object : ', obj)
print(' ', idx, ' Geometry : ', obj.Geometry)
print(' ', idx, ' Attributes : ', obj.Attributes)
print(' ', idx, ' Attributes.Id : ', obj.Attributes.Id)
print(' ', idx, ' Attributes.Name : ', obj.Attributes.Name)
print(' ', idx, ' Attributes.LayerIndex : ', obj.Attributes.LayerIndex)
print(' ', idx, ' Attributes.ObjectColor: ', obj.Attributes.ObjectColor)
print(' ', idx, ' Attributes.Visible : ', obj.Attributes.Visible)
print()
# if geom is PolylineCurve, print its details
if isinstance(obj.Geometry, rhino3dm.PolylineCurve):
print(' curve IsClosed :', obj.Geometry.IsClosed)
print(' curve IsPlanar :', obj.Geometry.IsPlanar())
print(' curve IsPolyline :', obj.Geometry.IsPolyline())
print()
print(' curve PointCount :', obj.Geometry.PointCount)
for i in range(obj.Geometry.PointCount):
print(' curve pt ', i, ' : ', obj.Geometry.Point(i))
# print(' curve pt ', i, ' : ', obj.Geometry.ToPolyline().PointAt(i)) -- alternative
print()
print(' curve SegmentCount :', obj.Geometry.ToPolyline().SegmentCount)
for i in range(obj.Geometry.ToPolyline().SegmentCount):
print(' segement ', i, ' : ', obj.Geometry.ToPolyline().SegmentAt(i))
print()
cnt_cable += 1
else:
pass
for idx, obj in enumerate(iter(object_table)):
if (obj.Attributes.LayerIndex == layer_idx_of_cable):
total_num_cable += 1
print('total num of cable: ', total_num_cable)
```
## read `text`
```{python}
layer_idx_of_text = 2
cnt_text = 0
max_num_obj = 3
total_num_text = 0
for idx, obj in enumerate(iter(object_table)):
if (obj.Attributes.LayerIndex == layer_idx_of_text) and (cnt_text < max_num_obj):
print('object ', idx, ' Object : ', obj)
print(' ', idx, ' Geometry : ', obj.Geometry)
print(' ', idx, ' Attributes : ', obj.Attributes)
print(' ', idx, ' Attributes.Id : ', obj.Attributes.Id)
print(' ', idx, ' Attributes.Name : ', obj.Attributes.Name)
print(' ', idx, ' Attributes.LayerIndex : ', obj.Attributes.LayerIndex)
print(' ', idx, ' Attributes.ObjectColor : ', obj.Attributes.ObjectColor)
print(' ', idx, ' Attributes.Visible : ', obj.Attributes.Visible)
print()
# if geom is AnnotationBase, print its PlainText
if isinstance(obj.Geometry, rhino3dm.AnnotationBase):
print(' Geometry.PlainText : ', obj.Geometry.PlainText)
print()
cnt_text += 1
for idx, obj in enumerate(iter(object_table)):
if (obj.Attributes.LayerIndex == layer_idx_of_text):
total_num_text += 1
print('total num of text: ', total_num_text)
```
## create dataclass `MyPt`
```{python}
@dataclass
class OatPt:
idx : int
point: rhino3dm.Point3d
name : str
```
## read `pt`
```python
# layer_idx_of_pt = 2
# cnt_pt = 0
# max_num_obj = 3
# total_num_pt = 0
#
# oaPt_list_1 = []
#
# for idx, obj in enumerate(iter(object_table)):
# if (obj.Attributes.LayerIndex == layer_idx_of_pt) and (cnt_pt < max_num_obj):
# print('object ', idx, ' Object : ', obj)
# print(' ', idx, ' Geometry : ', obj.Geometry)
# print(' ', idx, ' Attributes : ', obj.Attributes)
# print(' ', idx, ' Attributes.Id : ', obj.Attributes.Id)
# print(' ', idx, ' Attributes.Name : ', obj.Attributes.Name)
# print(' ', idx, ' Attributes.LayerIndex : ', obj.Attributes.LayerIndex)
# print(' ', idx, ' Attributes.ObjectColor : ', obj.Attributes.ObjectColor)
# print(' ', idx, ' Attributes.Visible : ', obj.Attributes.Visible)
# print()
#
# # if geom is Point, print its xyz
# if isinstance(obj.Geometry, rhino3dm.Point):
# pt = obj.Geometry.Location
# print(' Geometry.Location.X : ', pt.X)
# print(' Geometry.Location.Y : ', pt.Y)
# print(' Geometry.Location.Z : ', pt.Z)
#
# oatPt = OatPt(idx, pt, str(idx))
# oaPt_list_1.append(oatPt)
#
# print()
#
# for idx, obj in enumerate(iter(object_table)):
# if (obj.Attributes.LayerIndex == layer_idx_of_pt):
# total_num_pt += 1
#
# print('total num of pt: ', total_num_pt)
```
```{python}
# for idx, oatPt in enumerate(oaPt_list_1):
# print('myPt', idx, ' : ', oatPt)
# print(' type of idx in myPt:', type(oatPt.idx))
# print(' type of point in myPt:', type(oatPt.point))
# print(' type of name in myPt:', type(oatPt.name))
```
## read `TextDot`
```{python}
max_num_obj = 3
cnt_textDot = 0
total_num_textDot = 0
for idx, obj in enumerate(iter(object_table)):
obj_geom = obj.Geometry
obj_attr = obj.Attributes
if (isinstance(obj_geom, rhino3dm.TextDot)) and (cnt_textDot < max_num_obj):
print('textDot object ', idx, ' : ', obj_geom)
print(' position : ', obj_geom.Point)
print(' text : ', obj_geom.Text)
print(' Attributes object : ', obj_attr)
print(' attribs.Id : ', obj_attr.Id)
print(' attribs.Name : ', obj_attr.Name)
print(' attribs.LayerIndex : ', obj_attr.LayerIndex)
print(' attribs.ObjectColor : ', obj_attr.ObjectColor)
print(' attribs.Visible : ', obj_attr.Visible)
print()
cnt_textDot += 1
for idx, obj in enumerate(iter(object_table)):
if isinstance(obj.Geometry, rhino3dm.TextDot):
total_num_textDot += 1
print('total num of textDot: ', total_num_textDot)
```
## read `EarthAnchorPoint`
```{python}
# get the `Settings` object as a property of File3dm object
file3dm_settings = file3dm_obj.Settings
print('file3dm settings: ', file3dm_settings)
# get `EarthAnchorPoint` in settings
earthAnchorPoint = file3dm_settings.EarthAnchorPoint
print('EarthAnchorPoint: ', earthAnchorPoint)
print()
# check if the earthAnchorPoint is set or not
earthAnchorPoint_set_or_not = earthAnchorPoint.EarthLocationIsSet()
print('earthAnchorPoint is set: ', earthAnchorPoint_set_or_not)
# get lat and lon of earthAnchorPoint
earthAnchorPoint_lat = earthAnchorPoint.EarthBasepointLatitude
earthAnchorPoint_lon = earthAnchorPoint.EarthBasepointLongitude
print('earthAnchorPoint latitude : ', earthAnchorPoint_lat)
print('earthAnchorPoint longitude: ', earthAnchorPoint_lon)
print()
# get ModelBasePoint
modelBasePoint = earthAnchorPoint.ModelBasePoint
print('model base point: ', modelBasePoint)
# get `ModelToEarthTransform`
modelUnitSystem = file3dm_settings.ModelUnitSystem
print('modelUnitSystem : ', modelUnitSystem)
modelToEarthTransform = earthAnchorPoint.GetModelToEarthTransform(modelUnitSystem)
print('modelToEarthTransform : ', modelToEarthTransform)
# create a test pt
# test_pt = rhino3dm.Point3d(91.2724, 187.784, 0)
test_pt = rhino3dm.Point3d(140.492,236.638,0)
test_pt_transformed_in_lon_lat_ele = test_pt.Transform(modelToEarthTransform)
print('test pt transformed in lon, lat, ele: ', test_pt_transformed_in_lon_lat_ele)
```
:::: {.column-margin}
::: {.callout-tip title="File3dm.Settings" collapse="true"}
[**_property_** `rhino3dm.File3dm.Settings`](https://mcneel.github.io/rhino3dm/python/api/File3dm.html#rhino3dm.File3dm.Settings)
<br />
File3dmSettings: Settings include tolerance, and unit system, and defaults used for creating views and objects.
<br />
Return: A rhino3dm.File3dm.Settings object
:::
::: {.callout-tip title="rhino3dm.File3dmSettings" collapse="true"}
[**_class_** `rhino3dm.File3dm`](https://mcneel.github.io/rhino3dm/python/api/File3dmSettings.html)
<br />
Property:
- `ModelBasePoint`
- `EarthAnchorPoint`
- `ModelUnitSystem`
:::
::: {.callout-tip title="rhino3dm.EarthAnchorPoint" collapse="true"}
[**_class_** `rhino3dm.EarthAnchorPoint`](https://mcneel.github.io/rhino3dm/python/api/EarthAnchorPoint.html#module-rhino3dm)
Property:
- EarthBasepointLatitude
- EarthBasepointLongitude
- ModelBasePoint
- GetModelToEarthTransform(modelUnitSystem)
:::
::: {.callout-tip title="Set EarthAnchorPoint in Rhino" collapse="true"}
[**_cmd_** `Set EarthAnchorPoint in Rhino`](https://docs.mcneel.com/rhino/5/help/en-us/commands/earthanchorpoint.htm)
:::
::: {.callout-tip title="Rhino to Google Earth" collapse="true"}
[**_wiki_** `Rhino to Google Earth`](https://wiki.mcneel.com/rhino/googleearth)
:::
::::