topologicpy 0.4.36__py3-none-any.whl → 0.4.38__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
topologicpy/Topology.py CHANGED
@@ -905,7 +905,7 @@ class Topology():
905
905
 
906
906
 
907
907
  @staticmethod
908
- def BoundingBox(topology, optimize=0, axes="xyz"):
908
+ def BoundingBox(topology, optimize=0, axes="xyz", tolerance=0.0001):
909
909
  """
910
910
  Returns a cell representing a bounding box of the input topology. The returned cell contains a dictionary with keys "xrot", "yrot", and "zrot" that represents rotations around the X,Y, and Z axes. If applied in the order of Z,Y,Z, the resulting box will become axis-aligned.
911
911
 
@@ -917,9 +917,12 @@ class Topology():
917
917
  If set to an integer from 1 (low optimization) to 10 (high optimization), the method will attempt to optimize the bounding box so that it reduces its surface area. The default is 0 which will result in an axis-aligned bounding box. The default is 0.
918
918
  axes : str , optional
919
919
  Sets what axes are to be used for rotating the bounding box. This can be any permutation or substring of "xyz". It is not case sensitive. The default is "xyz".
920
+ tolerance : float , optional
921
+ The desired tolerance. The default is 0.0001.
922
+
920
923
  Returns
921
924
  -------
922
- topologic.Cell
925
+ topologic.Cell or topologic.Face
923
926
  The bounding box of the input topology.
924
927
 
925
928
  """
@@ -1050,17 +1053,16 @@ class Topology():
1050
1053
  vb3 = topologic.Vertex.ByCoordinates(maxX, maxY, minZ)
1051
1054
  vb4 = topologic.Vertex.ByCoordinates(minX, maxY, minZ)
1052
1055
 
1053
- vt1 = topologic.Vertex.ByCoordinates(minX, minY, maxZ)
1054
- vt2 = topologic.Vertex.ByCoordinates(maxX, minY, maxZ)
1055
- vt3 = topologic.Vertex.ByCoordinates(maxX, maxY, maxZ)
1056
- vt4 = topologic.Vertex.ByCoordinates(minX, maxY, maxZ)
1057
1056
  baseWire = Wire.ByVertices([vb1, vb2, vb3, vb4], close=True)
1058
1057
  baseFace = Face.ByWire(baseWire)
1059
- box = Cell.ByThickenedFace(baseFace, planarize=False, thickness=abs(maxZ-minZ), bothSides=False)
1058
+ if abs(maxZ-minZ) < tolerance:
1059
+ box = baseFace
1060
+ else:
1061
+ box = Cell.ByThickenedFace(baseFace, planarize=False, thickness=abs(maxZ-minZ), bothSides=False)
1060
1062
  box = Topology.Rotate(box, origin=origin, x=1,y=0,z=0, degree=-best_x)
1061
1063
  box = Topology.Rotate(box, origin=origin, x=0,y=1,z=0, degree=-best_y)
1062
1064
  box = Topology.Rotate(box, origin=origin, x=0,y=0,z=1, degree=-best_z)
1063
- dictionary = Dictionary.ByKeysValues(["xrot","yrot","zrot"], [best_x, best_y, best_z])
1065
+ dictionary = Dictionary.ByKeysValues(["xrot","yrot","zrot", "minx", "miny", "minz", "maxx", "maxy", "maxz", "width", "length", "height"], [best_x, best_y, best_z, minX, minY, minZ, maxX, maxY, maxZ, (maxX-minX), (maxY-minY), (maxZ-minZ)])
1064
1066
  box = Topology.SetDictionary(box, dictionary)
1065
1067
  return box
1066
1068
 
@@ -1515,122 +1517,359 @@ class Topology():
1515
1517
  The list of imported topologies. If the list only contains one element, it returns that element.
1516
1518
 
1517
1519
  """
1520
+
1518
1521
  from topologicpy.Dictionary import Dictionary
1522
+ from topologicpy.Context import Context
1523
+ from topologicpy.Vertex import Vertex
1524
+ from topologicpy.Edge import Edge
1525
+ from topologicpy.Wire import Wire
1526
+ from topologicpy.Face import Face
1527
+ from topologicpy.Shell import Shell
1528
+ from topologicpy.Cell import Cell
1529
+ from topologicpy.CellComplex import CellComplex
1530
+ from topologicpy.Cluster import Cluster
1531
+ from topologicpy.Aperture import Aperture
1519
1532
 
1520
- def processApertures(subTopologies, apertures, exclusive, tolerance):
1521
- usedTopologies = []
1522
- for subTopology in subTopologies:
1523
- usedTopologies.append(0)
1524
- ap = 1
1525
- for aperture in apertures:
1526
- apCenter = Topology.InternalVertex(aperture, tolerance)
1527
- for i in range(len(subTopologies)):
1528
- subTopology = subTopologies[i]
1529
- if exclusive == True and usedTopologies[i] == 1:
1530
- continue
1531
- if topologic.VertexUtility.Distance(apCenter, subTopology) < tolerance:
1532
- context = topologic.Context.ByTopologyParameters(subTopology, 0.5, 0.5, 0.5)
1533
- _ = topologic.Aperture.ByTopologyContext(aperture, context)
1534
- if exclusive == True:
1535
- usedTopologies[i] = 1
1536
- ap = ap + 1
1533
+
1534
+ def getUUID(topology, uuidKey="uuid"):
1535
+ d = Topology.Dictionary(topology)
1536
+ if d == None:
1537
+ uuidOne = str(uuid.uuid1())
1538
+ d = Dictionary.ByKeyValue(uuidKey, uuidOne)
1539
+ elif uuidKey not in Dictionary.Keys(d):
1540
+ uuidOne = str(uuid.uuid1())
1541
+ d = Dictionary.SetValueAtKey(d, uuidKey, uuidOne)
1542
+ topology = Topology.SetDictionary(topology, d)
1543
+ else:
1544
+ uuidOne = Dictionary.ValueAtKey(d, uuidKey)
1545
+ return uuidOne
1546
+
1547
+ def find_json_item(json_list, key, value):
1548
+ for item in json_list:
1549
+ if key in item and item[key] == value:
1550
+ return item
1537
1551
  return None
1538
1552
 
1539
- def getApertures(apertureList):
1540
- returnApertures = []
1541
- for item in apertureList:
1542
- aperture = Topology.ByBREPString(item['brep'])
1543
- dictionary = item['dictionary']
1544
- keys = list(dictionary.keys())
1545
- values = []
1546
- for key in keys:
1547
- values.append(dictionary[key])
1548
- topDictionary = Dictionary.ByKeysValues(keys, values)
1549
- if len(keys) > 0:
1550
- _ = aperture.SetDictionary(topDictionary)
1551
- returnApertures.append(aperture)
1552
- return returnApertures
1553
-
1554
- def assignDictionary(dictionary):
1555
- selector = dictionary['selector']
1556
- pydict = dictionary['dictionary']
1557
- v = topologic.Vertex.ByCoordinates(selector[0], selector[1], selector[2])
1558
- d = Dictionary.ByPythonDictionary(pydict)
1559
- _ = v.SetDictionary(d)
1553
+ def buildAperture(j_aperture):
1554
+ j_vertices = []
1555
+ j_edges = []
1556
+ j_wires = []
1557
+ j_faces = []
1558
+ j_shells = []
1559
+ j_cells = []
1560
+ j_cellComplexes = []
1561
+ for jsonItem in j_aperture:
1562
+ topology_type = jsonItem['type']
1563
+ if topology_type.lower() == "vertex":
1564
+ j_vertices.append(jsonItem)
1565
+ elif topology_type.lower() == "edge":
1566
+ j_edges.append(jsonItem)
1567
+ elif topology_type.lower() == "wire":
1568
+ j_wires.append(jsonItem)
1569
+ elif topology_type.lower() == "face":
1570
+ j_faces.append(jsonItem)
1571
+ elif topology_type.lower() == "shell":
1572
+ j_shells.append(jsonItem)
1573
+ elif topology_type.lower() == "cell":
1574
+ j_cells.append(jsonItem)
1575
+ elif topology_type.lower() == "cellcomplex":
1576
+ j_cellComplexes.append(jsonItem)
1577
+
1578
+ vertices = [buildVertex(j_v) for j_v in j_vertices]
1579
+ edges = [buildEdge(j_e, j_vertices, uuidKey="uuid") for j_e in j_edges]
1580
+ wires = [buildWire(j_w, j_edges, j_vertices, uuidKey="uuid") for j_w in j_wires]
1581
+ faces = [buildFace(j_f, j_wires, j_edges, j_vertices, uuidKey="uuid") for j_f in j_faces]
1582
+ shells = [buildShell(j_s, j_faces, j_wires, j_edges, j_vertices, uuidKey="uuid") for j_s in j_shells]
1583
+ cells = [buildCell(j_c, j_shells, j_faces, j_wires, j_edges, j_vertices, uuidKey="uuid") for j_c in j_cells]
1584
+ cellComplexes = [buildCellComplex(j_cc, j_cells, j_shells, j_faces, j_wires, j_edges, j_vertices, uuidKey="uuid") for j_cc in j_cellComplexes]
1585
+ if len(cellComplexes) > 0:
1586
+ everything = cellComplexes
1587
+ elif len(cells) > 0:
1588
+ everything = cells
1589
+ elif len(shells) > 0:
1590
+ everything = shells
1591
+ elif len(faces) > 0:
1592
+ everything = faces
1593
+ elif len(wires) > 0:
1594
+ everything = wires
1595
+ elif len(edges) > 0:
1596
+ everything = edges
1597
+ elif len(vertices) > 0:
1598
+ everything = vertices
1599
+ else:
1600
+ return None
1601
+ if len(everything) == 1:
1602
+ aperture = everything[0]
1603
+ else:
1604
+ aperture = Topology.SelfMerge(Cluster.ByTopologies(everything))
1605
+ return aperture
1606
+
1607
+ def buildVertex(json_item):
1608
+ x, y, z = json_item['coordinates']
1609
+ d = json_item['dictionary']
1610
+ v = Vertex.ByCoordinates(x,y,z)
1611
+ v = Topology.SetDictionary(v, Dictionary.ByPythonDictionary(d))
1612
+ apertures = [buildAperture(j_ap) for j_ap in json_item['apertures']]
1613
+ context = Context.ByTopologyParameters(v, u=0.5, v=0.5, w=0.5)
1614
+ for ap in apertures:
1615
+ _ = Aperture.ByTopologyContext(ap, context)
1560
1616
  return v
1561
1617
 
1562
- topology = None
1563
- if not string:
1564
- print("Topology.ByJSONString - Error: the input string is not a valid string. Returning None.")
1618
+ def buildEdge(json_item, j_vertices, uuidKey="uuid"):
1619
+ edge_vertices = json_item['vertices']
1620
+ vertices = []
1621
+ for j_v in edge_vertices:
1622
+ vertices.append(buildVertex(find_json_item(j_vertices, uuidKey, j_v)))
1623
+ e = Edge.ByVertices(vertices)
1624
+ d = json_item['dictionary']
1625
+ e = Topology.SetDictionary(e, Dictionary.ByPythonDictionary(d))
1626
+ apertures = [buildAperture(j_ap) for j_ap in json_item['apertures']]
1627
+ context = Context.ByTopologyParameters(e, u=0.5, v=0.5, w=0.5)
1628
+ for ap in apertures:
1629
+ _ = Aperture.ByTopologyContext(ap, context)
1630
+ return e
1631
+
1632
+ def buildWire(json_item, j_edges, j_vertices, uuidKey="uuid"):
1633
+ wire_edges = json_item['edges']
1634
+ edges = []
1635
+ for j_e in wire_edges:
1636
+ edges.append(buildEdge(find_json_item(j_edges, uuidKey, j_e), j_vertices, uuidKey=uuidKey))
1637
+ w = Wire.ByEdges(edges)
1638
+ d = json_item['dictionary']
1639
+ w = Topology.SetDictionary(w, Dictionary.ByPythonDictionary(d))
1640
+ apertures = [buildAperture(j_ap) for j_ap in json_item['apertures']]
1641
+ context = Context.ByTopologyParameters(w, u=0.5, v=0.5, w=0.5)
1642
+ for ap in apertures:
1643
+ _ = Aperture.ByTopologyContext(ap, context)
1644
+ return w
1645
+
1646
+ def buildFace(json_item, j_wires, j_edges, j_vertices, uuidKey="uuid"):
1647
+ face_wires = json_item['wires']
1648
+ wires = []
1649
+ external_boundary = buildWire(find_json_item(j_wires, uuidKey, face_wires[0]), j_edges, j_vertices, uuidKey=uuidKey)
1650
+ internal_boundaries = []
1651
+ for j_w in face_wires[1:]:
1652
+ ib = buildWire(find_json_item(j_wires, uuidKey, j_w),j_edges, j_vertices, uuidKey=uuidKey)
1653
+ internal_boundaries.append(ib)
1654
+ f = Face.ByWire(external_boundary)
1655
+ if Face.Area(f) < 0:
1656
+ f = Face.ByWires(Wire.Invert(external_boundary), internal_boundaries)
1657
+ else:
1658
+ f = Face.ByWires(external_boundary, internal_boundaries)
1659
+ d = json_item['dictionary']
1660
+ f = Topology.SetDictionary(f, Dictionary.ByPythonDictionary(d))
1661
+ apertures = [buildAperture(j_ap) for j_ap in json_item['apertures']]
1662
+ context = Context.ByTopologyParameters(f, u=0.5, v=0.5, w=0.5)
1663
+ for ap in apertures:
1664
+ _ = Aperture.ByTopologyContext(ap, context)
1665
+ return f
1666
+
1667
+ def buildShell(json_item, j_faces, j_wires, j_edges, j_vertices, uuidKey="uuid"):
1668
+ shell_faces = json_item['faces']
1669
+ faces = []
1670
+ for j_f in shell_faces:
1671
+ faces.append(buildFace(find_json_item(j_faces, uuidKey, j_f), j_wires, j_edges, j_vertices, uuidKey=uuidKey))
1672
+ s = Shell.ByFaces(faces)
1673
+ d = json_item['dictionary']
1674
+ s = Topology.SetDictionary(s, Dictionary.ByPythonDictionary(d))
1675
+ apertures = [buildAperture(j_ap) for j_ap in json_item['apertures']]
1676
+ context = Context.ByTopologyParameters(s, u=0.5, v=0.5, w=0.5)
1677
+ for ap in apertures:
1678
+ _ = Aperture.ByTopologyContext(ap, context)
1679
+ return s
1680
+
1681
+ def buildCell(json_item, j_shells, j_faces, j_wires, j_edges, j_vertices, uuidKey="uuid"):
1682
+ cell_shells = json_item['shells']
1683
+ shells = []
1684
+ external_boundary = buildShell(find_json_item(j_shells, uuidKey, cell_shells[0]), j_faces, j_wires, j_edges, j_vertices, uuidKey=uuidKey)
1685
+ internal_boundaries = []
1686
+ for j_s in cell_shells[1:]:
1687
+ internal_boundaries.append(buildShell(find_json_item(j_shells, uuidKey, j_s), j_faces, j_wires, j_edges, j_vertices, uuidKey=uuidKey))
1688
+ c = Cell.ByShell(external_boundary)
1689
+ for ib in internal_boundaries:
1690
+ ib_c = Cell.ByShell(ib)
1691
+ c = Topology.Difference(c, ib_c)
1692
+ d = json_item['dictionary']
1693
+ c = Topology.SetDictionary(c, Dictionary.ByPythonDictionary(d))
1694
+ apertures = [buildAperture(j_ap) for j_ap in json_item['apertures']]
1695
+ context = Context.ByTopologyParameters(c, u=0.5, v=0.5, w=0.5)
1696
+ for ap in apertures:
1697
+ _ = Aperture.ByTopologyContext(ap, context)
1698
+ return c
1699
+
1700
+ def buildCellComplex(json_item, j_cells, j_shells, j_faces, j_wires, j_edges, j_vertices, uuidKey="uuid"):
1701
+ cc_cells = json_item['cells']
1702
+ cells = []
1703
+ for j_c in cc_cells:
1704
+ cells.append(buildCell(find_json_item(j_cells, uuidKey, j_c), j_shells, j_faces, j_wires, j_edges, j_vertices, uuidKey=uuidKey))
1705
+ cc = CellComplex.ByCells(cells)
1706
+ d = json_item['dictionary']
1707
+ cc = Topology.SetDictionary(cc, Dictionary.ByPythonDictionary(d))
1708
+ apertures = [buildAperture(j_ap) for j_ap in json_item['apertures']]
1709
+ context = Context.ByTopologyParameters(cc, u=0.5, v=0.5, w=0.5)
1710
+ for ap in apertures:
1711
+ _ = Aperture.ByTopologyContext(ap, context)
1712
+ return cc
1713
+
1714
+ def addAperturesUUID(topology, uuidKey="uuid"):
1715
+ topology_apertures = [Aperture.ApertureTopology(ap) for ap in Topology.Apertures(topology)]
1716
+ apertures_uuid = []
1717
+ for top_a in topology_apertures:
1718
+ uuid = getUUID(top_a, uuidKey=uuidKey)
1719
+ apertures_uuid.append(uuid)
1720
+ d = Topology.Dictionary(topology)
1721
+ d = Dictionary.SetValueAtKey(d, 'apertures', apertures_uuid)
1722
+ topology = Topology.SetDictionary(topology, d)
1723
+ s = Topology.InternalVertex(topology)
1724
+ s = Topology.SetDictionary(s, d)
1725
+ return topology, s, topology_apertures
1726
+
1727
+ def findAperture(uuid, apertures, uuidKey="uuid"):
1728
+ for ap in apertures:
1729
+ d = Topology.Dictionary(ap)
1730
+ ap_uuid = Dictionary.ValueAtKey(d, uuidKey)
1731
+ if uuid == ap_uuid:
1732
+ return ap
1565
1733
  return None
1566
- topologies = []
1734
+
1735
+ def setApertures(topology, allApertures, uuidKey="uuid"):
1736
+ apertures = []
1737
+ d = Topology.Dictionary(topology)
1738
+ apertures_uuid = Dictionary.ValueAtKey(d, 'apertures')
1739
+ if not isinstance(apertures_uuid, list):
1740
+ apertures_uuid = [apertures_uuid]
1741
+ for ap_uuid in apertures_uuid:
1742
+ ap = findAperture(ap_uuid, allApertures, uuidKey=uuidKey)
1743
+ if ap != None:
1744
+ apertures.append(ap)
1745
+ context = Context.ByTopologyParameters(topology, u=0.5, v=0.5, w=0.5)
1746
+ for ap in apertures:
1747
+ _ = Aperture.ByTopologyContext(ap, context)
1748
+ return topology
1749
+
1750
+
1567
1751
  jsondata = json.loads(string)
1568
1752
  if not isinstance(jsondata, list):
1569
1753
  jsondata = [jsondata]
1754
+
1755
+ j_vertices = []
1756
+ j_edges = []
1757
+ j_wires = []
1758
+ j_faces = []
1759
+ j_shells = []
1760
+ j_cells = []
1761
+ j_cellComplexes = []
1762
+
1763
+ vertices = []
1764
+ edges = []
1765
+ wires = []
1766
+ faces = []
1767
+ shells = []
1768
+ cells = []
1769
+ cellComplexes = []
1770
+
1570
1771
  for jsonItem in jsondata:
1571
- brep = jsonItem['brep']
1572
- topology = Topology.ByBREPString(brep)
1573
- dictionary = jsonItem['dictionary']
1574
- topDictionary = Dictionary.ByPythonDictionary(dictionary)
1575
- topology = Topology.SetDictionary(topology, topDictionary)
1576
- cellApertures = getApertures(jsonItem['cellApertures'])
1577
- cells = []
1578
- try:
1579
- _ = topology.Cells(None, cells)
1580
- except:
1581
- pass
1582
- processApertures(cells, cellApertures, False, 0.001)
1583
- faceApertures = getApertures(jsonItem['faceApertures'])
1584
- faces = []
1585
- try:
1586
- _ = topology.Faces(None, faces)
1587
- except:
1588
- pass
1589
- processApertures(faces, faceApertures, False, 0.001)
1590
- edgeApertures = getApertures(jsonItem['edgeApertures'])
1591
- edges = []
1592
- try:
1593
- _ = topology.Edges(None, edges)
1594
- except:
1595
- pass
1596
- processApertures(edges, edgeApertures, False, 0.001)
1597
- vertexApertures = getApertures(jsonItem['vertexApertures'])
1598
- vertices = []
1599
- try:
1600
- _ = topology.Vertices(None, vertices)
1601
- except:
1602
- pass
1603
- processApertures(vertices, vertexApertures, False, 0.001)
1604
- cellDataList = jsonItem['cellDictionaries']
1605
- cellSelectors = []
1606
- for cellDataItem in cellDataList:
1607
- cellSelectors.append(assignDictionary(cellDataItem))
1608
- if len(cellSelectors) > 0:
1609
- topology = Topology.TransferDictionariesBySelectors(topology=topology, selectors=cellSelectors, tranVertices=False, tranEdges=False, tranFaces=False, tranCells=True, tolerance=0.0001)
1610
- faceDataList = jsonItem['faceDictionaries']
1611
- faceSelectors = []
1612
- for faceDataItem in faceDataList:
1613
- faceSelectors.append(assignDictionary(faceDataItem))
1614
- if len(faceSelectors) > 0:
1615
- topology = Topology.TransferDictionariesBySelectors(topology=topology, selectors=faceSelectors, tranVertices=False, tranEdges=False, tranFaces=True, tranCells=False, tolerance=0.0001)
1616
- edgeDataList = jsonItem['edgeDictionaries']
1617
- edgeSelectors = []
1618
- for edgeDataItem in edgeDataList:
1619
- edgeSelectors.append(assignDictionary(edgeDataItem))
1620
- if len(edgeSelectors) > 0:
1621
- topology = Topology.TransferDictionariesBySelectors(topology=topology, selectors=edgeSelectors, tranVertices=False, tranEdges=True, tranFaces=False, tranCells=False, tolerance=0.0001)
1622
- vertexDataList = jsonItem['vertexDictionaries']
1623
- vertexSelectors = []
1624
- for vertexDataItem in vertexDataList:
1625
- vertexSelectors.append(assignDictionary(vertexDataItem))
1626
- if len(vertexSelectors) > 0:
1627
- topology = Topology.TransferDictionariesBySelectors(topology=topology, selectors=vertexSelectors, tranVertices=True, tranEdges=False, tranFaces=False, tranCells=False, tolerance=0.0001)
1628
- topologies.append(topology)
1629
-
1630
- if len(topologies) == 1:
1631
- return topologies[0]
1772
+ topology_type = jsonItem['type']
1773
+ if topology_type.lower() == "vertex":
1774
+ j_vertices.append(jsonItem)
1775
+ elif topology_type.lower() == "edge":
1776
+ j_edges.append(jsonItem)
1777
+ elif topology_type.lower() == "wire":
1778
+ j_wires.append(jsonItem)
1779
+ elif topology_type.lower() == "face":
1780
+ j_faces.append(jsonItem)
1781
+ elif topology_type.lower() == "shell":
1782
+ j_shells.append(jsonItem)
1783
+ elif topology_type.lower() == "cell":
1784
+ j_cells.append(jsonItem)
1785
+ elif topology_type.lower() == "cellcomplex":
1786
+ j_cellComplexes.append(jsonItem)
1787
+
1788
+ vertices = [buildVertex(j_v) for j_v in j_vertices]
1789
+ vertex_selectors = []
1790
+ all_vertex_apertures = []
1791
+ for v in vertices:
1792
+ v, s, vertex_apertures = addAperturesUUID(v, uuidKey="uuid")
1793
+ all_vertex_apertures += vertex_apertures
1794
+ vertex_selectors.append(s)
1795
+
1796
+ edges = [buildEdge(j_e, j_vertices, uuidKey="uuid") for j_e in j_edges]
1797
+ edge_selectors = []
1798
+ all_edge_apertures = []
1799
+ for e in edges:
1800
+ e, s, edge_apertures = addAperturesUUID(e, uuidKey="uuid")
1801
+ all_edge_apertures += edge_apertures
1802
+ edge_selectors.append(s)
1803
+
1804
+ wires = [buildWire(j_w, j_edges, j_vertices, uuidKey="uuid") for j_w in j_wires]
1805
+ wire_selectors = []
1806
+ all_wire_apertures = []
1807
+ for w in wires:
1808
+ w, s, wire_apertures = addAperturesUUID(w, uuidKey="uuid")
1809
+ all_wire_apertures += wire_apertures
1810
+ wire_selectors.append(s)
1811
+
1812
+ faces = [buildFace(j_f, j_wires, j_edges, j_vertices, uuidKey="uuid") for j_f in j_faces]
1813
+ face_selectors = []
1814
+ all_face_apertures = []
1815
+ for f in faces:
1816
+ f, s, face_apertures = addAperturesUUID(f, uuidKey="uuid")
1817
+ all_face_apertures += face_apertures
1818
+ face_selectors.append(s)
1819
+
1820
+ shells = [buildShell(j_s, j_faces, j_wires, j_edges, j_vertices, uuidKey="uuid") for j_s in j_shells]
1821
+ shell_selectors = []
1822
+ all_shell_apertures = []
1823
+ for sh in shells:
1824
+ sh, s, shell_apertures = addAperturesUUID(sh, uuidKey="uuid")
1825
+ all_shell_apertures += shell_apertures
1826
+ shell_selectors.append(s)
1827
+
1828
+ cells = [buildCell(j_c, j_shells, j_faces, j_wires, j_edges, j_vertices, uuidKey="uuid") for j_c in j_cells]
1829
+ cell_selectors = []
1830
+ all_cell_apertures = []
1831
+ for c in cells:
1832
+ c, s, cell_apertures = addAperturesUUID(c, uuidKey="uuid")
1833
+ all_cell_apertures += cell_apertures
1834
+ cell_selectors.append(s)
1835
+
1836
+ cellComplexes = [buildCellComplex(j_cc, j_cells, j_shells, j_faces, j_wires, j_edges, j_vertices, uuidKey="uuid") for j_cc in j_cellComplexes]
1837
+ cellComplex_selectors = []
1838
+ all_cellComplex_apertures = []
1839
+ for cc in cellComplexes:
1840
+ cc, s, cellComplex_apertures = addAperturesUUID(cc, uuidKey="uuid")
1841
+ all_cellComplex_apertures += cellComplex_apertures
1842
+ cellComplex_selectors.append(s)
1843
+
1844
+ everything = vertices+edges+wires+faces+shells+cells+cellComplexes
1845
+ toplevelTopologies = []
1846
+ for ev in everything:
1847
+ d = Topology.Dictionary(ev)
1848
+ if Dictionary.ValueAtKey(d,'toplevel') == True:
1849
+ toplevelTopologies.append(ev)
1850
+
1851
+ for tp in toplevelTopologies:
1852
+ _ = Topology.TransferDictionariesBySelectors(tp, vertex_selectors, tranVertices=True, tolerance=tolerance)
1853
+ _ = Topology.TransferDictionariesBySelectors(tp, edge_selectors, tranEdges=True, tolerance=tolerance)
1854
+ _ = Topology.TransferDictionariesBySelectors(tp, face_selectors, tranFaces=True, tolerance=tolerance)
1855
+ _ = Topology.TransferDictionariesBySelectors(tp, cell_selectors, tranCells=True, tolerance=tolerance)
1856
+ tp_vertices = Topology.Vertices(tp)
1857
+ for tp_vertex in tp_vertices:
1858
+ tp_vertex = setApertures(tp_vertex, all_vertex_apertures, uuidKey="uuid")
1859
+ tp_edges = Topology.Edges(tp)
1860
+ for tp_edge in tp_edges:
1861
+ tp_edge = setApertures(tp_edge, all_edge_apertures, uuidKey="uuid")
1862
+ tp_faces = Topology.Faces(tp)
1863
+ for tp_face in tp_faces:
1864
+ tp_face = setApertures(tp_face, all_face_apertures, uuidKey="uuid")
1865
+ tp_cells = Topology.Cells(tp)
1866
+ for tp_cell in tp_cells:
1867
+ tp_cell = setApertures(tp_cell, all_cell_apertures, uuidKey="uuid")
1868
+
1869
+ if len(toplevelTopologies) == 1:
1870
+ return toplevelTopologies[0]
1632
1871
  else:
1633
- return topologies
1872
+ return toplevelTopologies
1634
1873
 
1635
1874
  @staticmethod
1636
1875
  def ByJSONPath(path, tolerance=0.0001):
@@ -1660,165 +1899,6 @@ class Topology():
1660
1899
  return None
1661
1900
  return Topology.ByJSONFile(file=file, tolerance=tolerance)
1662
1901
 
1663
- @staticmethod
1664
- def ByImportedJSONMK1(path, tolerance=0.0001):
1665
- """
1666
- DEPRECATED. DO NOT USE. Instead use Topology.ByJSONPath or Topology.ByJSONFile
1667
- Imports the topology from a JSON file.
1668
-
1669
- Parameters
1670
- ----------
1671
- path : str
1672
- The file path to the json file.
1673
- tolerance : float , optional
1674
- The desired tolerance. The default is 0.0001.
1675
-
1676
- Returns
1677
- -------
1678
- list
1679
- The list of imported topologies.
1680
-
1681
- """
1682
- print("Topology.ByImportedJSONMK1 - WARNING: This method is DEPRECATED. DO NOT USE. Instead use Topology.ByJSONPathMK1 or Topology.ByJSONFileMK1")
1683
- return Topology.ByJSONPath(path=path, tolerance=tolerance)
1684
-
1685
- @staticmethod
1686
- def ByImportedJSONMK2(path, tolerance=0.0001):
1687
- """
1688
- Imports the topology from a JSON file.
1689
-
1690
- Parameters
1691
- ----------
1692
- path : str
1693
- The file path to the json file.
1694
- tolerance : float , optional
1695
- The desired tolerance. The default is 0.0001.
1696
-
1697
- Returns
1698
- -------
1699
- list
1700
- The list of imported topologies.
1701
-
1702
- """
1703
- from topologicpy.Dictionary import Dictionary
1704
- def getApertures(apertureList, folderPath):
1705
- returnApertures = []
1706
- for item in apertureList:
1707
- brepFileName = item['brep']
1708
- breppath = os.path.join(folderPath, brepFileName+".brep")
1709
- brepFile = open(breppath)
1710
- if brepFile:
1711
- brepString = brepFile.read()
1712
- aperture = Topology.ByBREPString(brepString)
1713
- brepFile.close()
1714
- dictionary = item['dictionary']
1715
- keys = list(dictionary.keys())
1716
- values = []
1717
- for key in keys:
1718
- values.append(dictionary[key])
1719
- topDictionary = Dictionary.ByKeysValues(keys, values)
1720
- if len(keys) > 0:
1721
- _ = aperture.SetDictionary(topDictionary)
1722
- returnApertures.append(aperture)
1723
- return returnApertures
1724
-
1725
- def processApertures(subTopologies, apertures, exclusive, tolerance):
1726
- usedTopologies = []
1727
- for subTopology in subTopologies:
1728
- usedTopologies.append(0)
1729
- ap = 1
1730
- for aperture in apertures:
1731
- apCenter = Topology.InternalVertex(aperture, tolerance)
1732
- for i in range(len(subTopologies)):
1733
- subTopology = subTopologies[i]
1734
- if exclusive == True and usedTopologies[i] == 1:
1735
- continue
1736
- if topologic.VertexUtility.Distance(apCenter, subTopology) < tolerance:
1737
- context = topologic.Context.ByTopologyParameters(subTopology, 0.5, 0.5, 0.5)
1738
- _ = topologic.Aperture.ByTopologyContext(aperture, context)
1739
- if exclusive == True:
1740
- usedTopologies[i] = 1
1741
- ap = ap + 1
1742
- return None
1743
-
1744
- def assignDictionary(dictionary):
1745
- selector = dictionary['selector']
1746
- pydict = dictionary['dictionary']
1747
- v = topologic.Vertex.ByCoordinates(selector[0], selector[1], selector[2])
1748
- d = Dictionary.ByPythonDictionary(pydict)
1749
- _ = v.SetDictionary(d)
1750
- return v
1751
-
1752
- topology = None
1753
- jsonFile = open(path)
1754
- folderPath = os.path.dirname(path)
1755
- if jsonFile:
1756
- topologies = []
1757
- jsondata = json.load(jsonFile)
1758
- for jsonItem in jsondata:
1759
- brepFileName = jsonItem['brep']
1760
- breppath = os.path.join(folderPath, brepFileName+".brep")
1761
- brepFile = open(breppath)
1762
- if brepFile:
1763
- brepString = brepFile.read()
1764
- topology = Topology.ByBREPString(brepString)
1765
- brepFile.close()
1766
- dictionary = jsonItem['dictionary']
1767
- topDictionary = Dictionary.ByPythonDictionary(dictionary)
1768
- _ = topology.SetDictionary(topDictionary)
1769
- cellApertures = getApertures(jsonItem['cellApertures'], folderPath)
1770
- cells = []
1771
- try:
1772
- _ = topology.Cells(None, cells)
1773
- except:
1774
- pass
1775
- processApertures(cells, cellApertures, False, 0.001)
1776
- faceApertures = getApertures(jsonItem['faceApertures'], folderPath)
1777
- faces = []
1778
- try:
1779
- _ = topology.Faces(None, faces)
1780
- except:
1781
- pass
1782
- processApertures(faces, faceApertures, False, 0.001)
1783
- edgeApertures = getApertures(jsonItem['edgeApertures'], folderPath)
1784
- edges = []
1785
- try:
1786
- _ = topology.Edges(None, edges)
1787
- except:
1788
- pass
1789
- processApertures(edges, edgeApertures, False, 0.001)
1790
- vertexApertures = getApertures(jsonItem['vertexApertures'], folderPath)
1791
- vertices = []
1792
- try:
1793
- _ = topology.Vertices(None, vertices)
1794
- except:
1795
- pass
1796
- processApertures(vertices, vertexApertures, False, 0.001)
1797
- cellDataList = jsonItem['cellDictionaries']
1798
- cellSelectors = []
1799
- for cellDataItem in cellDataList:
1800
- cellSelectors.append(assignDictionary(cellDataItem))
1801
- Topology.TransferDictionariesBySelectors(topology, cellSelectors, tranVertices=False, tranEdges=False, tranFaces=False, tranCells=True, tolerance=tolerance)
1802
- faceDataList = jsonItem['faceDictionaries']
1803
- faceSelectors = []
1804
- for faceDataItem in faceDataList:
1805
- faceSelectors.append(assignDictionary(faceDataItem))
1806
- Topology.TransferDictionariesBySelectors(topology, faceSelectors, tranVertices=False, tranEdges=False, tranFaces=True, tranCells=False, tolerance=tolerance)
1807
- edgeDataList = jsonItem['edgeDictionaries']
1808
- edgeSelectors = []
1809
- for edgeDataItem in edgeDataList:
1810
- edgeSelectors.append(assignDictionary(edgeDataItem))
1811
- Topology.TransferDictionariesBySelectors(topology, edgeSelectors, tranVertices=False, tranEdges=True, tranFaces=False, tranCells=False, tolerance=tolerance)
1812
- vertexDataList = jsonItem['vertexDictionaries']
1813
- vertexSelectors = []
1814
- for vertexDataItem in vertexDataList:
1815
- vertexSelectors.append(assignDictionary(vertexDataItem))
1816
- Topology.TransferDictionariesBySelectors(topology, vertexSelectors, tranVertices=True, tranEdges=False, tranFaces=False, tranCells=False, tolerance=tolerance)
1817
- topologies.append(topology)
1818
- return topologies
1819
- print("Topology.ByImportedJSONMK2 - Error: the input file is not a valid file. Returning None.")
1820
- return None
1821
-
1822
1902
  @staticmethod
1823
1903
  def ByOBJString(string, transposeAxes = True, progressBar=False, renderer="notebook", tolerance=0.0001):
1824
1904
  """
@@ -2876,7 +2956,7 @@ class Topology():
2876
2956
  return ''
2877
2957
  '''
2878
2958
  @staticmethod
2879
- def ExportToJSON(topologies, path, version=3, overwrite=False, tolerance=0.0001):
2959
+ def ExportToJSON(topologies, path, overwrite=False):
2880
2960
  """
2881
2961
  Exports the input list of topologies to a JSON file.
2882
2962
 
@@ -2886,12 +2966,8 @@ class Topology():
2886
2966
  The input list of topologies.
2887
2967
  path : str
2888
2968
  The path to the JSON file.
2889
- version : int , optional
2890
- The OCCT BRep version to use. Options are 1,2,or 3. The default is 3.
2891
2969
  overwrite : bool , optional
2892
2970
  If set to True the ouptut file will overwrite any pre-existing file. Otherwise, it won't. The default is False.
2893
- tolerance : float , optional
2894
- The desired tolerance. The default is 0.0001.
2895
2971
 
2896
2972
  Returns
2897
2973
  -------
@@ -2916,12 +2992,20 @@ class Topology():
2916
2992
  except:
2917
2993
  raise Exception("Error: Could not create a new file at the following location: "+path)
2918
2994
  if (f):
2919
- jsondata = json.loads(Topology.JSONString(topologies, version=version, tolerance=tolerance))
2920
- json.dump(jsondata, f, indent=4, sort_keys=True)
2921
- f.close()
2995
+ jsondata = json.loads(Topology.JSONString(topologies))
2996
+ if jsondata != None:
2997
+ json.dump(jsondata, f, indent=4, sort_keys=True)
2998
+ f.close()
2999
+ return True
3000
+ else:
3001
+ f.close()
3002
+ return False
3003
+ return False
3004
+
3005
+
2922
3006
 
2923
3007
  @staticmethod
2924
- def JSONString(topologies, version=3, tolerance=0.0001):
3008
+ def JSONString(topologies):
2925
3009
  """
2926
3010
  Exports the input list of topologies to a JSON string
2927
3011
 
@@ -2929,12 +3013,6 @@ class Topology():
2929
3013
  ----------
2930
3014
  topologies : list or topologic.Topology
2931
3015
  The input list of topologies or a single topology.
2932
- path : str
2933
- The path to the JSON file.
2934
- version : int , optional
2935
- The OCCT BRep version to use. Options are 1,2,or 3. The default is 3.
2936
- tolerance : float , optional
2937
- The desired tolerance. The default is 0.0001.
2938
3016
 
2939
3017
  Returns
2940
3018
  -------
@@ -2943,329 +3021,234 @@ class Topology():
2943
3021
 
2944
3022
  """
2945
3023
 
3024
+ from topologicpy.Vertex import Vertex
3025
+ from topologicpy.Edge import Edge
3026
+ from topologicpy.Face import Face
3027
+ from topologicpy.Cell import Cell
2946
3028
  from topologicpy.Dictionary import Dictionary
2947
3029
 
2948
- def cellAperturesAndDictionaries(topology, tolerance=0.0001):
2949
- cells = []
2950
- try:
2951
- _ = topology.Cells(None, cells)
2952
- except:
2953
- return [[],[],[]]
2954
- cellApertures = []
2955
- cellDictionaries = []
2956
- cellSelectors = []
2957
- for aCell in cells:
2958
- tempApertures = []
2959
- _ = aCell.Apertures(tempApertures)
2960
- for anAperture in tempApertures:
2961
- cellApertures.append(anAperture)
2962
- cellDictionary = Dictionary.PythonDictionary(Topology.Dictionary(aCell))
2963
- if len(cellDictionary.keys()) > 0:
2964
- cellDictionaries.append(cellDictionary)
2965
- iv = topologic.CellUtility.InternalVertex(aCell, tolerance)
2966
- cellSelectors.append([iv.X(), iv.Y(), iv.Z()])
2967
- return [cellApertures, cellDictionaries, cellSelectors]
2968
-
2969
- def faceAperturesAndDictionaries(topology, tolerance=0.0001):
2970
- faces = []
2971
- try:
2972
- _ = topology.Faces(None, faces)
2973
- except:
2974
- return [[],[],[]]
2975
- faceApertures = []
2976
- faceDictionaries = []
2977
- faceSelectors = []
2978
- for aFace in faces:
2979
- tempApertures = []
2980
- _ = aFace.Apertures(tempApertures)
2981
- for anAperture in tempApertures:
2982
- faceApertures.append(anAperture)
2983
- faceDictionary = Dictionary.PythonDictionary(Topology.Dictionary(aFace))
2984
- if len(faceDictionary.keys()) > 0:
2985
- faceDictionaries.append(faceDictionary)
2986
- iv = topologic.FaceUtility.InternalVertex(aFace, tolerance)
2987
- faceSelectors.append([iv.X(), iv.Y(), iv.Z()])
2988
- return [faceApertures, faceDictionaries, faceSelectors]
2989
-
2990
- def edgeAperturesAndDictionaries(topology):
2991
- edges = []
2992
- try:
2993
- _ = topology.Edges(None, edges)
2994
- except:
2995
- return [[],[],[]]
2996
- edgeApertures = []
2997
- edgeDictionaries = []
2998
- edgeSelectors = []
2999
- for anEdge in edges:
3000
- tempApertures = []
3001
- _ = anEdge.Apertures(tempApertures)
3002
- for anAperture in tempApertures:
3003
- edgeApertures.append(anAperture)
3004
- edgeDictionary = Dictionary.PythonDictionary(Topology.Dictionary(anEdge))
3005
- if len(edgeDictionary.keys()) > 0:
3006
- edgeDictionaries.append(edgeDictionary)
3007
- iv = topologic.EdgeUtility.PointAtParameter(anEdge, 0.5)
3008
- edgeSelectors.append([iv.X(), iv.Y(), iv.Z()])
3009
- return [edgeApertures, edgeDictionaries, edgeSelectors]
3010
-
3011
- def vertexAperturesAndDictionaries(topology):
3012
- vertices = []
3013
- try:
3014
- _ = topology.Vertices(None, vertices)
3015
- except:
3016
- return [[],[],[]]
3017
- vertexApertures = []
3018
- vertexDictionaries = []
3019
- vertexSelectors = []
3020
- for aVertex in vertices:
3021
- tempApertures = []
3022
- _ = aVertex.Apertures(tempApertures)
3023
- for anAperture in tempApertures:
3024
- vertexApertures.append(anAperture)
3025
- vertexDictionary = Dictionary.PythonDictionary(Topology.Dictionary(aVertex))
3026
- if len(vertexDictionary.keys()) > 0:
3027
- vertexDictionaries.append(vertexDictionary)
3028
- vertexSelectors.append([aVertex.X(), aVertex.Y(), aVertex.Z()])
3029
- return [vertexApertures, vertexDictionaries, vertexSelectors]
3030
-
3031
- def apertureDicts(apertureList):
3032
- apertureDicts = []
3033
- for anAperture in apertureList:
3034
- apertureData = {}
3035
- apertureData['brep'] = Topology.BREPString(Aperture.Topology(anAperture))
3036
- apertureData['dictionary'] = Dictionary.PythonDictionary(Topology.Dictionary(anAperture))
3037
- apertureDicts.append(apertureData)
3038
- return apertureDicts
3039
-
3040
- def subTopologyDicts(dicts, selectors):
3041
- returnDicts = []
3042
- for i in range(len(dicts)):
3043
- data = {}
3044
- data['dictionary'] = dicts[i]
3045
- data['selector'] = selectors[i]
3046
- returnDicts.append(data)
3047
- return returnDicts
3048
-
3049
- def getTopologyData(topology, version=3, tolerance=0.0001):
3030
+ def getUUID(topology, uuidKey="uuid"):
3031
+ d = Topology.Dictionary(topology)
3032
+ if d == None:
3033
+ print("Dictionary is None for", topology)
3034
+ if uuidKey not in Dictionary.Keys(d):
3035
+ uuidOne = str(uuid.uuid1())
3036
+ d = Dictionary.SetValueAtKey(d, uuidKey, uuidOne)
3037
+ topology = Topology.SetDictionary(topology, d)
3038
+ else:
3039
+ uuidOne = Dictionary.ValueAtKey(d, uuidKey)
3040
+ return uuidOne
3041
+
3042
+ def getVertex(topology, uuidKey="uuid"):
3050
3043
  returnDict = {}
3051
- brep = Topology.BREPString(topology, version=version)
3052
- dictionary = Dictionary.PythonDictionary(Topology.Dictionary(topology))
3053
- returnDict['brep'] = brep
3054
- returnDict['dictionary'] = dictionary
3055
- cellApertures, cellDictionaries, cellSelectors = cellAperturesAndDictionaries(topology, tolerance=tolerance)
3056
- faceApertures, faceDictionaries, faceSelectors = faceAperturesAndDictionaries(topology, tolerance=tolerance)
3057
- edgeApertures, edgeDictionaries, edgeSelectors = edgeAperturesAndDictionaries(topology)
3058
- vertexApertures, vertexDictionaries, vertexSelectors = vertexAperturesAndDictionaries(topology)
3059
- returnDict['cellApertures'] = apertureDicts(cellApertures)
3060
- returnDict['faceApertures'] = apertureDicts(faceApertures)
3061
- returnDict['edgeApertures'] = apertureDicts(edgeApertures)
3062
- returnDict['vertexApertures'] = apertureDicts(vertexApertures)
3063
- returnDict['cellDictionaries'] = subTopologyDicts(cellDictionaries, cellSelectors)
3064
- returnDict['faceDictionaries'] = subTopologyDicts(faceDictionaries, faceSelectors)
3065
- returnDict['edgeDictionaries'] = subTopologyDicts(edgeDictionaries, edgeSelectors)
3066
- returnDict['vertexDictionaries'] = subTopologyDicts(vertexDictionaries, vertexSelectors)
3044
+ uuidOne = getUUID(topology, uuidKey=uuidKey)
3045
+ returnDict['type'] = "Vertex"
3046
+ returnDict['uuid'] = uuidOne
3047
+ returnDict['coordinates'] = [Vertex.X(topology), Vertex.Y(topology), Vertex.Z(topology)]
3048
+ returnDict['dictionary'] = Dictionary.PythonDictionary(Topology.Dictionary(topology))
3067
3049
  return returnDict
3068
3050
 
3069
- if not isinstance(topologies,list):
3070
- return json.dumps(getTopologyData(topologies, version=version, tolerance=tolerance))
3071
- elif len(topologies) == 1:
3072
- return json.dumps(getTopologyData(topologies[0], version=version, tolerance=tolerance))
3073
- jsondata = []
3074
- for topology in topologies:
3075
- jsondata.append(getTopologyData(topology, version=version, tolerance=tolerance))
3076
- return json.dumps(jsondata, indent=4, sort_keys=True)
3077
-
3078
- @staticmethod
3079
- def ExportToJSONMK2(topologies, folderPath, fileName, version=3, overwrite=False, tolerance=0.0001):
3080
- """
3081
- Export the input list of topologies to a JSON file
3082
-
3083
- Parameters
3084
- ----------
3085
- topologies : list
3086
- The input list of topologies.
3087
- folderPath : list
3088
- The path to the folder containing the json file and brep files.
3089
- fileName : str
3090
- The name of the JSON file.
3091
- version : int , optional
3092
- The OCCT BRep version to use. Options are 1,2,or 3. The default is 3.
3093
- overwrite : bool , optional
3094
- If set to True, any existing file will be overwritten. The default is False.
3095
- tolerance : float , optional
3096
- The desired tolerance. The default is 0.0001.
3051
+ def getEdge(topology, uuidKey="uuid"):
3052
+ returnDict = {}
3053
+ uuidOne = getUUID(topology, uuidKey=uuidKey)
3054
+ returnDict['type'] = "Edge"
3055
+ returnDict['uuid'] = uuidOne
3056
+ returnDict['vertices'] = [getUUID(v, uuidKey=uuidKey) for v in Edge.Vertices(topology)]
3057
+ returnDict['dictionary'] = Dictionary.PythonDictionary(Topology.Dictionary(topology))
3058
+ return returnDict
3097
3059
 
3098
- Returns
3099
- -------
3100
- bool
3101
- The status of exporting the JSON file. If True, the operation was successful. Otherwise, it was unsuccesful.
3060
+ def getWire(topology, uuidKey="uuid"):
3061
+ returnDict = {}
3062
+ uuidOne = getUUID(topology, uuidKey="uuid")
3063
+ returnDict['type'] = "Wire"
3064
+ returnDict['uuid'] = uuidOne
3065
+ returnDict['edges'] = [getUUID(e, uuidKey=uuidKey) for e in Topology.Edges(topology)]
3066
+ returnDict['dictionary'] = Dictionary.PythonDictionary(Topology.Dictionary(topology))
3067
+ return returnDict
3102
3068
 
3103
- """
3069
+ def getFace(topology, uuidKey="uuid"):
3070
+ apertures = [Aperture.ApertureTopology(ap) for ap in Topology.Apertures(topology)]
3071
+ returnDict = {}
3072
+ uuidOne = getUUID(topology, uuidKey=uuidKey)
3073
+ returnDict['type'] = "Face"
3074
+ returnDict['uuid'] = uuidOne
3075
+ wires = []
3076
+ external_boundary = Face.ExternalBoundary(topology)
3077
+ wires.append(getUUID(Face.ExternalBoundary(topology), uuidKey=uuidKey))
3078
+ internal_boundaries = [getUUID(ib, uuidKey=uuidKey) for ib in Face.InternalBoundaries(topology)]
3079
+ wires += internal_boundaries
3080
+ returnDict['wires'] = wires
3081
+ dictionary = Dictionary.PythonDictionary(Topology.Dictionary(topology))
3082
+ returnDict['dictionary'] = dictionary
3083
+ return returnDict
3104
3084
 
3105
- from topologicpy.Dictionary import Dictionary
3085
+ def getShell(topology, uuidKey="uuid"):
3086
+ returnDict = {}
3087
+ uuidOne = getUUID(topology, uuidKey=uuidKey)
3088
+ returnDict['type'] = "Shell"
3089
+ returnDict['uuid'] = uuidOne
3090
+ returnDict['faces'] = [getUUID(f, uuidKey=uuidKey) for f in Topology.Faces(topology)]
3091
+ returnDict['dictionary'] = Dictionary.PythonDictionary(Topology.Dictionary(topology))
3092
+ return returnDict
3106
3093
 
3107
- def cellAperturesAndDictionaries(topology, tolerance=0.0001):
3108
- if topology.Type() <= 32:
3109
- return [[],[],[]]
3110
- cells = []
3111
- try:
3112
- _ = topology.Cells(None, cells)
3113
- except:
3114
- return [[],[],[]]
3115
- cellApertures = []
3116
- cellDictionaries = []
3117
- cellSelectors = []
3118
- for aCell in cells:
3119
- tempApertures = []
3120
- _ = aCell.Apertures(tempApertures)
3121
- for anAperture in tempApertures:
3122
- cellApertures.append(anAperture)
3123
- cellDictionary = Dictionary.PythonDictionary(Topology.Dictionary(aCell))
3124
- if len(cellDictionary.keys()) > 0:
3125
- cellDictionaries.append(cellDictionary)
3126
- iv = topologic.CellUtility.InternalVertex(aCell, tolerance)
3127
- cellSelectors.append([iv.X(), iv.Y(), iv.Z()])
3128
- return [cellApertures, cellDictionaries, cellSelectors]
3129
-
3130
- def faceAperturesAndDictionaries(topology, tolerance=0.0001):
3131
- if topology.Type() <= 8:
3132
- return [[],[],[]]
3133
- faces = []
3134
- try:
3135
- _ = topology.Faces(None, faces)
3136
- except:
3137
- return [[],[],[]]
3138
- faceApertures = []
3139
- faceDictionaries = []
3140
- faceSelectors = []
3141
- for aFace in faces:
3142
- tempApertures = []
3143
- _ = aFace.Apertures(tempApertures)
3144
- for anAperture in tempApertures:
3145
- faceApertures.append(anAperture)
3146
- faceDictionary = Dictionary.PythonDictionary(Topology.Dictionary(aFace))
3147
- if len(faceDictionary.keys()) > 0:
3148
- faceDictionaries.append(faceDictionary)
3149
- iv = topologic.FaceUtility.InternalVertex(aFace, tolerance)
3150
- faceSelectors.append([iv.X(), iv.Y(), iv.Z()])
3151
- return [faceApertures, faceDictionaries, faceSelectors]
3152
-
3153
- def edgeAperturesAndDictionaries(topology):
3154
- if topology.Type() <= 2:
3155
- return [[],[],[]]
3156
- edges = []
3157
- try:
3158
- _ = topology.Edges(None, edges)
3159
- except:
3160
- return [[],[],[]]
3161
- edgeApertures = []
3162
- edgeDictionaries = []
3163
- edgeSelectors = []
3164
- for anEdge in edges:
3165
- tempApertures = []
3166
- _ = anEdge.Apertures(tempApertures)
3167
- for anAperture in tempApertures:
3168
- edgeApertures.append(anAperture)
3169
- edgeDictionary = Dictionary.PythonDictionary(Topology.Dictionary(anEdge))
3170
- if len(edgeDictionary.keys()) > 0:
3171
- edgeDictionaries.append(edgeDictionary)
3172
- iv = topologic.EdgeUtility.PointAtParameter(anEdge, 0.5)
3173
- edgeSelectors.append([iv.X(), iv.Y(), iv.Z()])
3174
- return [edgeApertures, edgeDictionaries, edgeSelectors]
3175
-
3176
- def vertexAperturesAndDictionaries(topology):
3177
- if topology.Type() <= 1:
3178
- return [[],[],[]]
3179
- vertices = []
3180
- try:
3181
- _ = topology.Vertices(None, vertices)
3182
- except:
3183
- return [[],[],[]]
3184
- vertexApertures = []
3185
- vertexDictionaries = []
3186
- vertexSelectors = []
3187
- for aVertex in vertices:
3188
- tempApertures = []
3189
- _ = aVertex.Apertures(tempApertures)
3190
- for anAperture in tempApertures:
3191
- vertexApertures.append(anAperture)
3192
- vertexDictionary = Dictionary.PythonDictionary(Topology.Dictionary(aVertex))
3193
- if len(vertexDictionary.keys()) > 0:
3194
- vertexDictionaries.append(vertexDictionary)
3195
- vertexSelectors.append([aVertex.X(), aVertex.Y(), aVertex.Z()])
3196
- return [vertexApertures, vertexDictionaries, vertexSelectors]
3197
-
3198
-
3199
- def apertureDicts(apertureList, brepName, folderPath, version=3):
3200
- apertureDicts = []
3201
- for index, anAperture in enumerate(apertureList):
3202
- apertureName = brepName+"_aperture_"+str(index+1).zfill(5)
3203
- breppath = os.path.join(folderPath, apertureName+".brep")
3204
- brepFile = open(breppath, "w")
3205
- brepFile.write(Topology.BREPString(anAperture, version=version))
3206
- brepFile.close()
3207
- apertureData = {}
3208
- apertureData['brep'] = apertureName
3209
- apertureData['dictionary'] = Dictionary.PythonDictionary(Topology.Dictionary(anAperture))
3210
- apertureDicts.append(apertureData)
3211
- return apertureDicts
3212
-
3213
- def subTopologyDicts(dicts, selectors):
3214
- returnDicts = []
3215
- for i in range(len(dicts)):
3216
- data = {}
3217
- data['dictionary'] = dicts[i]
3218
- data['selector'] = selectors[i]
3219
- returnDicts.append(data)
3220
- return returnDicts
3221
-
3222
- def getTopologyData(topology, brepName, folderPath, version=3, tolerance=0.0001):
3094
+ def getCell(topology, uuidKey="uuid"):
3223
3095
  returnDict = {}
3096
+ uuidOne = getUUID(topology, uuidKey=uuidKey)
3097
+ returnDict['type'] = "Cell"
3098
+ returnDict['uuid'] = uuidOne
3099
+ shells = []
3100
+ external_boundary = Cell.ExternalBoundary(topology)
3101
+ shells.append(getUUID(external_boundary, uuidKey=uuidKey))
3102
+ internal_boundaries = [getUUID(ib, uuidKey=uuidKey) for ib in Cell.InternalBoundaries(topology)]
3103
+ shells += internal_boundaries
3104
+ returnDict['shells'] = shells
3224
3105
  dictionary = Dictionary.PythonDictionary(Topology.Dictionary(topology))
3225
- returnDict['brep'] = brepName
3226
3106
  returnDict['dictionary'] = dictionary
3227
- cellApertures, cellDictionaries, cellSelectors = cellAperturesAndDictionaries(topology, tolerance=tolerance)
3228
- faceApertures, faceDictionaries, faceSelectors = faceAperturesAndDictionaries(topology, tolerance=tolerance)
3229
- edgeApertures, edgeDictionaries, edgeSelectors = edgeAperturesAndDictionaries(topology)
3230
- vertexApertures, vertexDictionaries, vertexSelectors = vertexAperturesAndDictionaries(topology)
3231
- returnDict['cellApertures'] = apertureDicts(cellApertures, brepName, folderPath, version)
3232
- returnDict['faceApertures'] = apertureDicts(faceApertures, brepName, folderPath, version)
3233
- returnDict['edgeApertures'] = apertureDicts(edgeApertures, brepName, folderPath, version)
3234
- returnDict['vertexApertures'] = apertureDicts(vertexApertures, brepName, folderPath, version)
3235
- returnDict['cellDictionaries'] = subTopologyDicts(cellDictionaries, cellSelectors)
3236
- returnDict['faceDictionaries'] = subTopologyDicts(faceDictionaries, faceSelectors)
3237
- returnDict['edgeDictionaries'] = subTopologyDicts(edgeDictionaries, edgeSelectors)
3238
- returnDict['vertexDictionaries'] = subTopologyDicts(vertexDictionaries, vertexSelectors)
3239
3107
  return returnDict
3240
-
3241
- if not (isinstance(topologies,list)):
3108
+
3109
+ def getCellComplex(topology, uuidKey="uuid"):
3110
+ returnDict = {}
3111
+ uuidOne = getUUID(topology, uuidKey=uuidKey)
3112
+ returnDict['type'] = "CellComplex"
3113
+ returnDict['uuid'] = uuidOne
3114
+ returnDict['cells'] = [getUUID(c, uuidKey=uuidKey) for c in Topology.Cells(topology)]
3115
+ returnDict['dictionary'] = Dictionary.PythonDictionary(Topology.Dictionary(topology))
3116
+ return returnDict
3117
+
3118
+
3119
+ def getApertureData(topology, topLevel="False", uuidKey="uuid"):
3120
+ json_data = []
3121
+ if isinstance(topology, topologic.Vertex):
3122
+ d = getVertex(topology, uuidKey=uuidKey)
3123
+ elif isinstance(topology, topologic.Edge):
3124
+ d = getEdge(topology, uuidKey=uuidKey)
3125
+ elif isinstance(topology, topologic.Wire):
3126
+ d = getWire(topology, uuidKey=uuidKey)
3127
+ elif isinstance(topology, topologic.Face):
3128
+ d = getFace(topology, uuidKey=uuidKey)
3129
+ elif isinstance(topology, topologic.Shell):
3130
+ d = getShell(topology, uuidKey=uuidKey)
3131
+ elif isinstance(topology, topologic.Cell):
3132
+ d = getCell(topology, uuidKey=uuidKey)
3133
+ elif isinstance(topology, topologic.CellComplex):
3134
+ d = getCellComplex(topology, uuidKey=uuidKey)
3135
+ d['dictionary']['toplevel'] = topLevel
3136
+ json_data += getSubTopologyData(topology, uuidKey=uuidKey)
3137
+ apertures = [Aperture.ApertureTopology(ap) for ap in Topology.Apertures(topology)]
3138
+ aperture_data = []
3139
+ for ap in apertures:
3140
+ aperture_data.append(getApertureData(ap, topLevel=False, uuidKey=uuidKey))
3141
+ d['apertures'] = aperture_data
3142
+ json_data.append(d)
3143
+ return json_data
3144
+
3145
+ def getSubTopologyData(topology, uuidKey="uuid"):
3146
+ json_data = []
3147
+ vertices = Topology.Vertices(topology)
3148
+ for v in vertices:
3149
+ d = getVertex(v, uuidKey=uuidKey)
3150
+ d['dictionary']['toplevel'] = False
3151
+ apertures = [Aperture.ApertureTopology(ap) for ap in Topology.Apertures(v)]
3152
+ aperture_data = []
3153
+ for ap in apertures:
3154
+ aperture_data.append(getApertureData(ap, topLevel=False, uuidKey=uuidKey))
3155
+ d['apertures'] = aperture_data
3156
+ json_data.append(d)
3157
+ edges = Topology.Edges(topology)
3158
+ for e in edges:
3159
+ d = getEdge(e, uuidKey=uuidKey)
3160
+ d['dictionary']['toplevel'] = False
3161
+ apertures = [Aperture.ApertureTopology(ap) for ap in Topology.Apertures(e)]
3162
+ aperture_data = []
3163
+ for ap in apertures:
3164
+ aperture_data.append(getApertureData(ap, topLevel=False, uuidKey=uuidKey))
3165
+ d['apertures'] = aperture_data
3166
+ json_data.append(d)
3167
+ wires = Topology.Wires(topology)
3168
+ for w in wires:
3169
+ d = getWire(w, uuidKey=uuidKey)
3170
+ d['dictionary']['toplevel'] = False
3171
+ apertures = [Aperture.ApertureTopology(ap) for ap in Topology.Apertures(w)]
3172
+ aperture_data = []
3173
+ for ap in apertures:
3174
+ aperture_data.append(getApertureData(ap, topLevel=False, uuidKey=uuidKey))
3175
+ d['apertures'] = aperture_data
3176
+ json_data.append(d)
3177
+ faces = Topology.Faces(topology)
3178
+ for f in faces:
3179
+ d = getFace(f, uuidKey=uuidKey)
3180
+ d['dictionary']['toplevel'] = False
3181
+ apertures = [Aperture.ApertureTopology(ap) for ap in Topology.Apertures(f)]
3182
+ aperture_data = []
3183
+ for ap in apertures:
3184
+ aperture_data.append(getApertureData(ap, topLevel=False, uuidKey=uuidKey))
3185
+ d['apertures'] = aperture_data
3186
+ json_data.append(d)
3187
+ shells = Topology.Shells(topology)
3188
+ for s in shells:
3189
+ d = getShell(s, uuidKey=uuidKey)
3190
+ d['dictionary']['toplevel'] = False
3191
+ apertures = [Aperture.ApertureTopology(ap) for ap in Topology.Apertures(s)]
3192
+ aperture_data = []
3193
+ for ap in apertures:
3194
+ aperture_data.append(getApertureData(ap, topLevel=False, uuidKey=uuidKey))
3195
+ d['apertures'] = aperture_data
3196
+ json_data.append(d)
3197
+ cells = Topology.Cells(topology)
3198
+ for c in cells:
3199
+ d = getCell(c, uuidKey=uuidKey)
3200
+ d['dictionary']['toplevel'] = False
3201
+ apertures = [Aperture.ApertureTopology(ap) for ap in Topology.Apertures(c)]
3202
+ aperture_data = []
3203
+ for ap in apertures:
3204
+ aperture_data.append(getApertureData(ap, topLevel=False, uuidKey=uuidKey))
3205
+ d['apertures'] = aperture_data
3206
+ json_data.append(d)
3207
+ cellComplexes = Topology.CellComplexes(topology)
3208
+ for cc in cellComplexes:
3209
+ d = getCellComplex(cc, uuidKey=uuidKey)
3210
+ d['dictionary']['toplevel'] = False
3211
+ apertures = [Aperture.ApertureTopology(ap) for ap in Topology.Apertures(cc)]
3212
+ aperture_data = []
3213
+ for ap in apertures:
3214
+ aperture_data.append(getApertureData(ap, topLevel=False, uuidKey=uuidKey))
3215
+ d['apertures'] = aperture_data
3216
+ json_data.append(d)
3217
+ return json_data
3218
+
3219
+ def getJSONData(topology, topLevel=False, uuidKey="uuid"):
3220
+ json_data = []
3221
+ if isinstance(topology, topologic.Vertex):
3222
+ d = getVertex(topology, uuidKey=uuidKey)
3223
+ elif isinstance(topology, topologic.Edge):
3224
+ d = getEdge(topology, uuidKey=uuidKey)
3225
+ elif isinstance(topology, topologic.Wire):
3226
+ d = getWire(topology, uuidKey=uuidKey)
3227
+ elif isinstance(topology, topologic.Face):
3228
+ d = getFace(topology, uuidKey=uuidKey)
3229
+ elif isinstance(topology, topologic.Shell):
3230
+ d = getShell(topology, uuidKey=uuidKey)
3231
+ elif isinstance(topology, topologic.Cell):
3232
+ d = getCell(topology, uuidKey=uuidKey)
3233
+ elif isinstance(topology, topologic.CellComplex):
3234
+ d = getCellComplex(topology, uuidKey=uuidKey)
3235
+ d['dictionary']['toplevel'] = topLevel
3236
+ json_data += getSubTopologyData(topology, uuidKey=uuidKey)
3237
+ apertures = [Aperture.ApertureTopology(ap) for ap in Topology.Apertures(topology)]
3238
+ aperture_data = []
3239
+ for ap in apertures:
3240
+ aperture_data.append(getApertureData(ap, topLevel=False, uuidKey=uuidKey))
3241
+ d['apertures'] = aperture_data
3242
+ json_data.append(d)
3243
+ return json_data
3244
+ json_data = []
3245
+ if not isinstance(topologies, list):
3242
3246
  topologies = [topologies]
3243
- # Make sure the file extension is .json
3244
- ext = fileName[len(fileName)-5:len(fileName)]
3245
- if ext.lower() != ".json":
3246
- fileName = fileName+".json"
3247
- jsonFile = None
3248
- jsonpath = os.path.join(folderPath, fileName)
3249
- try:
3250
- if overwrite == True:
3251
- jsonFile = open(jsonpath, "w")
3252
- else:
3253
- jsonFile = open(jsonpath, "x") # Try to create a new File
3254
- except:
3255
- raise Exception("Error: Could not create a new file at the following location: "+jsonpath)
3256
- if (jsonpath):
3257
- jsondata = []
3258
- for index, topology in enumerate(topologies):
3259
- brepName = "topology_"+str(index+1).zfill(5)
3260
- breppath = os.path.join(folderPath, brepName+".brep")
3261
- brepFile = open(breppath, "w")
3262
- brepFile.write(Topology.BREPString(topology, version=version))
3263
- brepFile.close()
3264
- jsondata.append(getTopologyData(topology, brepName, folderPath, version=version, tolerance=tolerance))
3265
- json.dump(jsondata, jsonFile, indent=4, sort_keys=True)
3266
- jsonFile.close()
3267
- return True
3268
- return False
3247
+ topologies = [x for x in topologies if isinstance(x, topologic.Topology)]
3248
+ for topology in topologies:
3249
+ json_data += getJSONData(topology, topLevel=True, uuidKey="uuid")
3250
+ json_string = json.dumps(json_data, indent=4, sort_keys=False)
3251
+ return json_string
3269
3252
 
3270
3253
  @staticmethod
3271
3254
  def OBJString(topology, transposeAxes=True):
@@ -3654,40 +3637,39 @@ class Topology():
3654
3637
  A vertex guaranteed to be inside the input topology.
3655
3638
 
3656
3639
  """
3640
+ from topologicpy.Vertex import Vertex
3641
+ from topologicpy.Edge import Edge
3642
+ from topologicpy.Face import Face
3643
+ from topologicpy.Cell import Cell
3657
3644
  if not isinstance(topology, topologic.Topology):
3658
3645
  print("Topology.InternalVertex - Error: the input topology is not a valid topology. Returning None.")
3659
3646
  return None
3660
3647
  vst = None
3661
3648
  classType = topology.Type()
3662
3649
  if classType == 64: #CellComplex
3663
- tempCells = []
3664
- _ = topology.Cells(tempCells)
3665
- tempCell = tempCells[0]
3666
- vst = topologic.CellUtility.InternalVertex(tempCell, tolerance)
3650
+ tempCell = Topology.Cells(topology)[0]
3651
+ vst = Cell.InternalVertex(tempCell, tolerance)
3667
3652
  elif classType == 32: #Cell
3668
- vst = topologic.CellUtility.InternalVertex(topology, tolerance)
3653
+ vst = Cell.InternalVertex(topology, tolerance)
3669
3654
  elif classType == 16: #Shell
3670
- tempFaces = []
3671
- _ = topology.Faces(None, tempFaces)
3672
- tempFace = tempFaces[0]
3673
- vst = topologic.FaceUtility.InternalVertex(tempFace, tolerance)
3655
+ tempFace = Topology.Faces(topology)[0]
3656
+ vst = Face.InternalVertex(tempFace, tolerance)
3674
3657
  elif classType == 8: #Face
3675
- vst = topologic.FaceUtility.InternalVertex(topology, tolerance)
3658
+ vst = Face.InternalVertex(topology, tolerance)
3676
3659
  elif classType == 4: #Wire
3677
3660
  if topology.IsClosed():
3678
3661
  internalBoundaries = []
3679
3662
  tempFace = topologic.Face.ByExternalInternalBoundaries(topology, internalBoundaries)
3680
- vst = topologic.FaceUtility.InternalVertex(tempFace, tolerance)
3663
+ vst = Face.InternalVertex(tempFace, tolerance)
3681
3664
  else:
3682
- tempEdges = []
3683
- _ = topology.Edges(None, tempEdges)
3684
- vst = topologic.EdgeUtility.PointAtParameter(tempEdges[0], 0.5)
3665
+ tempEdge = Topology.Edges(topology)[0]
3666
+ vst = Edge.VertexByParameter(tempEdge, 0.5)
3685
3667
  elif classType == 2: #Edge
3686
- vst = topologic.EdgeUtility.PointAtParameter(topology, 0.5)
3668
+ vst = Edge.VertexByParameter(topology, 0.5)
3687
3669
  elif classType == 1: #Vertex
3688
3670
  vst = topology
3689
3671
  else:
3690
- vst = topology.Centroid()
3672
+ vst = Topology.Centroid(topology)
3691
3673
  return vst
3692
3674
 
3693
3675
  @staticmethod
@@ -4155,14 +4137,27 @@ class Topology():
4155
4137
  The relevant selector.
4156
4138
 
4157
4139
  """
4140
+ from topologicpy.Edge import Edge
4141
+ from topologicpy.Face import Face
4142
+ from topologicpy.Cell import Cell
4143
+
4158
4144
  if topology.Type() == topologic.Vertex.Type():
4159
4145
  return topology
4160
4146
  elif topology.Type() == topologic.Edge.Type():
4161
- return topologic.EdgeUtility.PointAtParameter(topology, 0.5)
4147
+ return Edge.VertexByParameter(topology, 0.5)
4148
+ elif topology.Type() == topologic.Wire.Type():
4149
+ e = Topology.Edges(topology)[0]
4150
+ return Edge.VertexByParameter(e, 0.5)
4162
4151
  elif topology.Type() == topologic.Face.Type():
4163
- return topologic.FaceUtility.InternalVertex(topology, tolerance)
4152
+ return Face.InternalVertex(topology, tolerance)
4153
+ elif topology.Type() == topologic.Shell.Type():
4154
+ f = Topology.Faces(topology)[0]
4155
+ return Face.InternalVertex(f, tolerance)
4164
4156
  elif topology.Type() == topologic.Cell.Type():
4165
- return topologic.CellUtility.InternalVertex(topology, tolerance)
4157
+ return Cell.InternalVertex(topology, tolerance)
4158
+ elif topology.Type() == topologic.CellComplex.Type():
4159
+ c = Topology.Cells(topology)[0]
4160
+ return Cell.InternalVertex(c, tolerance)
4166
4161
  else:
4167
4162
  return topology.CenterOfMass()
4168
4163