def snappy_json(self): objects = [o for o in bpy.context.visible_objects if not self.exclude_object(o)] # Make a dictionary for default surface properties default_surface_dict = { 'level': { 'min': self.defaultLevel, 'max': self.defaultLevel } } # Make a dictionary of the surfaces surface_dict = { foamUtils.formatObjectName(o.name): { 'level': { 'min': o.ODS_CFD.mesh.meshMinLevel, 'max': o.ODS_CFD.mesh.meshMaxLevel } } for o in objects } # Make a dictionary of the refinement regions refinements = [ { 'name': foamUtils.formatObjectName(o.name), 'details': { 'mode': o.ODS_CFD.mesh.refinementMode, 'levels': o.ODS_CFD.mesh.distanceLevels } } for o in bpy.context.visible_objects if o.ODS_CFD.mesh.makeRefinementRegion ] # Overrides keep_point = self.get_unique_object('cfdMeshKeepPoint') overrides_dict = { 'castellatedMeshControls': { 'locationInMesh': [co for co in keep_point.location] } } # json dictionary json_dict = { 'overrides': overrides_dict, 'default_surface': default_surface_dict, 'surfaces': surface_dict, 'refinementRegions': refinements } return json_dict
def writeBody(self): f = self.f sc = bpy.context.scene if 'porous' in sc.ODS_CFD.solver.name: kp = bpy.data.objects['cfdMeshKeepPoint'] for o in bpy.context.selected_objects: if o.ODS_CFD.porous_isPorous: zoneName = foamUtils.formatObjectName(o.name) f.write("cellSet %s new surfaceToCell \"%s.stl\" 1((%f %f %f)) true true false -1 -100\n"%(zoneName, zoneName, kp.location[0], kp.location[1], kp.location[2]))
def get_set_set(self, obj, keep_point=[0, 0, 0], locations=[True, True, False]): return { 'name': foamUtils.formatObjectName(obj.name), 'locations': locations, 'keep_point': keep_point }
def writeBody(self): f = self.f f.write("faceSet nonPlanarFanFaces new labelToFace (1 1 1)\n") f.write("faceSet nonPlanarFanFaces clear\n") for o in bpy.context.selected_objects: if o.ODS_CFD.patchType == 'fan': globalNorm = o.matrix_world.to_3x3() * o.data.polygons[0].normal patchName = foamUtils.formatObjectName(o.name) f.write("faceSet currentFan new patchToFace " + patchName + "\n") f.write("faceSet currentFan delete normalToFace (%f %f %f) 0.001\n"%(globalNorm[0], globalNorm[1], globalNorm[2])) f.write("faceSet currentFan delete normalToFace (%f %f %f) 0.001\n"%(-1*globalNorm[0], -1*globalNorm[1], -1*globalNorm[2])) f.write("faceSet nonPlanarFanFaces add faceToFace currentFan\n") f.write("faceSet currentFan clear\n")
def writeObjectsToFile(f, objects=None, writePorous=True, writeNonPorous=True): """ Write objects in STL format to a file-like object """ if objects is None: objects = bpy.context.visible_objects specialNames = [ 'cfdBoundingBox', 'cfdMeshKeepPoint', 'MinX', 'MaxX', 'MinY', 'MaxY', 'MinZ', 'MaxZ' ] for obj in objects: # Check if we should skip this object if (not obj.type == 'MESH') or (len(obj.data.polygons) == 0): continue passedPorousWriteCheck = porousWriteCheck(obj, writePorous, writeNonPorous) if obj.type == 'MESH' and (obj.name.split('.')[0] not in specialNames ) and passedPorousWriteCheck: # Object.to_mesh() is not guaranteed to return a mesh. try: me = obj.to_mesh() except RuntimeError: continue # Transform to global coordinates and get the vertices me.transform(obj.matrix_world) vertices = me.vertices f.write("solid %s\n" % (foamUtils.formatObjectName(obj.name))) me.calc_loop_triangles() for face in me.loop_triangles: # Get the global coordinates of the vertices face_vertex_coords = [ vertices[index].co.copy() for index in face.vertices ] # faceToTriangles: Guarantee the face is a triangle - split if a quad for tri in faceToTriangles(face_vertex_coords): # Write faces to file (this is normally only one element in tris # unless faceToTriangles split a quad to 2 triangles) write_triangle(f, tri) f.write("endsolid\n")
def upload_separate_surfaces(self): system_settings = bpy.context.scene.ODS_CFD.system task_id = system_settings.task_id print("Writing refinement regions") separate_objects = [ obj for obj in bpy.context.visible_objects if self._separate_stl(obj) ] for obj in separate_objects: name = foamUtils.formatObjectName(obj.name) print(f" - Writing separate stl region: {name}") geometry = io.StringIO() asciiSTLExport.writeObjectsToFile(geometry, objects=[obj]) geometry.seek(0) print(" -- Cast refinement regions and cell sets to STL format") response = GenericViewSet( f'/api/task/{task_id}/file/foam/constant/triSurface/{name}.stl/' ).update(None, geometry.read().encode('utf8'), raw=True)
def writeBody(self): f=self.f # Get the number of porous zones cc = 0 for obj in bpy.context.selected_objects: if obj.ODS_CFD.porous_isPorous: cc += 1 # Write the number of porous zones header f.write("%i\n("%cc) # Write porousZone information for obj in bpy.context.selected_objects: if obj.ODS_CFD.porous_isPorous: zoneName = foamUtils.formatObjectName(obj.name) # Get axis orientation xa = obj.data.vertices[0].co.copy() za = obj.data.vertices[0].co.copy() xa[0]=1.0;xa[1]=0.0;xa[2]=0.0 za[0]=0.0;za[1]=0.0;za[2]=1.0 xa = obj.matrix_world.to_3x3()*xa xa.normalize() za = obj.matrix_world.to_3x3()*za za.normalize() # Get coefficients dc = obj.ODS_CFD.porous_Dcoeff fc = obj.ODS_CFD.porous_Fcoeff f.write(""" {0} {{ coordinateSystem {{ e1 ( {1} {2} {3} ); e2 ( {4} {5} {6} ); }} Darcy {{ d d [0 -2 0 0 0 0 0] ( {7} {8} {9} ); f f [0 -1 0 0 0 0 0] ( {10} {11} {12} ); }} }} """.format(zoneName,xa[0],xa[1],xa[2],za[0],za[1],za[2],dc[0],dc[1],dc[2],fc[0],fc[1],fc[2]))
def upload_setset(self): system_settings = bpy.context.scene.ODS_CFD.system task_id = system_settings.task_id cellset_objects = [ obj for obj in bpy.context.visible_objects if obj.ODS_CFD.mesh.makeCellSet ] if not cellset_objects: return None setset_str = "" for obj in cellset_objects: name = foamUtils.formatObjectName(obj.name) setset_str += f'cellSet {name} new surfaceToCell "constant/triSurface/{name}.stl" ((0 0 0)) true true false 0 0\n' print(f" - Writing separate setSet file:\n\n {setset_str}") response = GenericViewSet( f'/api/task/{task_id}/file/foam/cellSets.setSet/').update( None, setset_str.encode('utf8'), raw=True)
def to_json(self): objects = bpy.context.visible_objects skip_names = ['cfdBoundingBox','cfdMeshKeepPoint'] bcs = { formatObjectName(obj.name): obj.ODS_CFD.to_json() for obj in objects if not obj.name in skip_names } json_dict = { 'solver': self.name, 'boundary_conditions': bcs, 'overrides': { 'setup': [self.override_setup.as_string()] if self.override_setup else [], 'presets': [self.override_presets.as_string()] if self.override_presets else [], 'fields': [self.override_fields.as_string()] if self.override_fields else [], 'caseFiles': [self.override_case_files.as_string()] if self.override_case_files else [] } } return json_dict
def writeBody(self): specialNames = [ 'cfdBoundingBox', 'cfdMeshKeepPoint', 'MinX', 'MaxX', 'MinY', 'MaxY', 'MinZ', 'MaxZ' ] sc = bpy.context.scene obs = bpy.context.selected_objects f = self.f m = sc.ODS_CFD.mesh # Which of the steps to run self.writeString("castellatedMesh %s;\n" % (str.lower(str(m.castellated)))) self.writeString("snap %s;\n" % (str.lower(str(m.snap)))) self.writeString("addLayers %s;\n" % (str.lower(str(m.addLayers)))) # Geometry. Definition of all surfaces. self.writeString("\ngeometry\n{\n") self.writeString("cfdGeom.stl\n", indent=4) self.writeString("{\n", indent=4) self.writeString("type triSurfaceMesh;\n", indent=8) self.writeString("name cfdGeom;\n", indent=8) self.writeString("regions\n", indent=8) self.writeString("{\n", indent=8) for o in obs: obname = foamUtils.formatObjectName(o.name) if (o.type == 'MESH'): if (len(o.data.polygons) > 0) and ( not obname in specialNames) and o.ODS_CFD.doMesh and ( not o.ODS_CFD.porous_isPorous) and ( not o.ODS_CFD.mesh.makeRefinementRegion): self.writeString("%s { name %s ; }\n" % (obname, obname), indent=12) self.writeString("}\n", indent=8) self.writeString("}\n", indent=4) # Write Refinement Region Objects for o in obs: obname = foamUtils.formatObjectName(o.name) if (o.type == 'MESH'): if (len(o.data.polygons) > 0) and (not obname in specialNames ) and o.ODS_CFD.mesh.makeRefinementRegion: self.writeString("%s.stl\n" % (obname), indent=4) self.writeString("{\n", indent=4) self.writeString("type triSurfaceMesh;\n", indent=8) self.writeString("name %s;\n" % (obname), indent=8) self.writeString("}\n", indent=4) self.writeString("};\n") #------------------ # castellatedMeshControls self.writeString("castellatedMeshControls\n{\n") self.writeString("maxLocalCells " + str(m.maxLocalCells), indent=4, endl=";\n\n") self.writeString("maxGlobalCells " + str(m.maxGlobalCells), indent=4, endl=";\n\n") self.writeString("minRefinementCells " + str(m.minRefinementCells), indent=4, endl=";\n\n") self.writeString("nCellsBetweenLevels " + str(m.nCellsBetweenLevels), indent=4, endl=";\n\n") if m.nFeatureSnapIter > 0: self.writeString( "features ( { file \"cfdGeom.extendedFeatureEdgeMesh\"; level 0; } )", indent=4, endl=";\n\n") else: self.writeString("features ( )", indent=4, endl=";\n\n") # Surface based refinement self.writeString("refinementSurfaces\n", indent=4) self.writeString("{\n", indent=4) self.writeString("cfdGeom\n", indent=8) self.writeString("{\n", indent=8) self.writeString("level (%i %i);\n" % (m.defaultLevel, m.defaultLevel), indent=12) self.writeString("regions\n", indent=12) self.writeString("{\n", indent=12) for o in obs: obname = foamUtils.formatObjectName(o.name) if (o.type == 'MESH'): if (len(o.data.polygons) > 0) and ( not obname in specialNames) and o.ODS_CFD.doMesh and ( not o.ODS_CFD.porous_isPorous) and ( not o.ODS_CFD.mesh.makeRefinementRegion): minLevel = o.ODS_CFD.mesh.meshMinLevel maxLevel = o.ODS_CFD.mesh.meshMaxLevel if maxLevel < minLevel: maxLevel = minLevel self.writeString("%s { level (%i %i) ; }\n" % (obname, minLevel, maxLevel), indent=16) self.writeString("}\n", indent=12) self.writeString("}\n", indent=8) # Surface based refinement regions for o in obs: obname = foamUtils.formatObjectName(o.name) if (o.type == 'MESH'): if (len(o.data.polygons) > 0) and (not obname in specialNames ) and o.ODS_CFD.mesh.makeRefinementRegion: if o.ODS_CFD.mesh.refinementMode == "surface": self.writeString("%s\n" % (obname), indent=8) self.writeString("{\n", indent=8) self.writeString("level %s;\n" % (o.ODS_CFD.mesh.distanceLevels), indent=12) self.writeString("cellZone %s;\n" % (obname), indent=12) self.writeString("faceZone %s_faces;\n" % (obname), indent=12) self.writeString("}\n", indent=8) self.writeString("}\n\n", indent=4) # Resolve sharp angles self.writeString("resolveFeatureAngle %f;\n\n" % (degrees(m.resolveFeatureAngle)), indent=4) # Refinement regions self.writeString("refinementRegions {\n", indent=4) for o in obs: obname = foamUtils.formatObjectName(o.name) if (o.type == 'MESH'): if (len(o.data.polygons) > 0) and (not obname in specialNames ) and o.ODS_CFD.mesh.makeRefinementRegion: if not o.ODS_CFD.mesh.refinementMode == "surface": self.writeString("%s\n" % (obname), indent=8) self.writeString("{\n", indent=8) self.writeString("mode %s;\n" % (o.ODS_CFD.mesh.refinementMode), indent=12) self.writeString("levels %s;\n" % (o.ODS_CFD.mesh.distanceLevels), indent=12) self.writeString("}\n", indent=8) self.writeString("};\n\n", indent=4) # Mesh selection kp = bpy.data.objects['cfdMeshKeepPoint'] self.writeString("locationInMesh (%f %f %f);\n" % (kp.location[0], kp.location[1], kp.location[2]), indent=4) self.writeString("allowFreeStandingZoneFaces true;\n", indent=4) # Close castellatedMeshControls self.writeString("}\n\n") #------------------ # snapControls self.writeString("snapControls\n{\n") self.writeString("nSmoothPatch %i" % (m.nSmoothPatch), indent=4, endl=";\n\n") self.writeString("tolerance %f" % (m.tolerance), indent=4, endl=";\n\n") self.writeString("nSolveIter %i" % (m.nSolveIter), indent=4, endl=";\n\n") self.writeString("nRelaxIter %i" % (m.nRelaxIter), indent=4, endl=";\n\n") # Edge snap controls self.writeString("nFeatureSnapIter %i" % (m.nFeatureSnapIter), indent=4, endl=";\n\n") self.writeString("implicitFeatureSnap %s" % (str.lower(str(m.implicitFeatureSnap))), indent=4, endl=";\n\n") self.writeString("explicitFeatureSnap %s" % (str.lower(str(m.explicitFeatureSnap))), indent=4, endl=";\n\n") self.writeString("multiRegionFeatureSnap %s" % (str.lower(str(m.multiRegionFeatureSnap))), indent=4, endl=";\n\n") self.writeString("}\n\n") #------------------ # addLayersControls self.writeString("addLayersControls\n{\n") self.writeString("relativeSizes %s" % (str.lower(str(m.relativeSizes))), indent=4, endl=";\n\n") self.writeString("layers", indent=4, endl="\n") self.writeString("{", indent=4, endl="\n") for o in obs: obname = foamUtils.formatObjectName(o.name) if not obname in specialNames and o.type == 'MESH' and o.ODS_CFD.doMesh and ( not o.ODS_CFD.porous_isPorous) and ( not o.ODS_CFD.mesh.makeRefinementRegion): if o.ODS_CFD.mesh.nSurfaceLayers > 0: self.writeString(obname + " { nSurfaceLayers " + str(o.ODS_CFD.mesh.nSurfaceLayers) + " ; }\n", indent=8) self.writeString("}", indent=4, endl=";\n\n") self.writeString("expansionRatio " + str(m.expansionRatio), indent=4, endl=";\n\n") self.writeString("finalLayerThickness " + str(m.finalLayerThickness), indent=4, endl=";\n\n") self.writeString("minThickness " + str(m.minThickness), indent=4, endl=";\n\n") self.writeString("nGrow " + str(m.nGrow), indent=4, endl=";\n\n") # addLayersControls self.writeString("featureAngle " + str(degrees(m.featureAngle)), indent=4, endl=";\n\n") self.writeString("nRelaxIter " + str(m.nRelaxIterLayer), indent=4, endl=";\n\n") self.writeString("nSmoothSurfaceNormals " + str(m.nSmoothSurfaceNormals), indent=4, endl=";\n\n") self.writeString("nSmoothNormals " + str(m.nSmoothNormals), indent=4, endl=";\n\n") self.writeString("nSmoothThickness " + str(m.nSmoothThickness), indent=4, endl=";\n\n") self.writeString("maxFaceThicknessRatio " + str(m.maxFaceThicknessRatio), indent=4, endl=";\n\n") self.writeString("maxThicknessToMedialRatio " + str(m.maxThicknessToMedialRatio), indent=4, endl=";\n\n") self.writeString("minMedianAxisAngle " + str(degrees(m.minMedianAxisAngle)), indent=4, endl=";\n\n") self.writeString("nBufferCellsNoExtrude " + str(m.nBufferCellsNoExtrude), indent=4, endl=";\n\n") self.writeString("nLayerIter " + str(m.nLayerIter), indent=4, endl=";\n\n") f.write("}\n\n") #------------------ # Advanced Settings f.write("meshQualityControls\n{\n") self.writeString("maxNonOrtho 65", indent=4, endl=";\n\n") self.writeString("maxBoundarySkewness 20", indent=4, endl=";\n\n") self.writeString("maxInternalSkewness 4", indent=4, endl=";\n\n") self.writeString("maxConcave 80", indent=4, endl=";\n\n") self.writeString("minFlatness 0.5", indent=4, endl=";\n\n") self.writeString("minVol 1e-13", indent=4, endl=";\n\n") self.writeString("minTetQuality 1e-30", indent=4, endl=";\n\n") self.writeString("minArea -1", indent=4, endl=";\n\n") self.writeString("minTwist 0.05", indent=4, endl=";\n\n") self.writeString("minDeterminant 0.001", indent=4, endl=";\n\n") self.writeString("minFaceWeight 0.05", indent=4, endl=";\n\n") self.writeString("minVolRatio 0.01", indent=4, endl=";\n\n") self.writeString("minTriangleTwist -1", indent=4, endl=";\n\n") # Advanced self.writeString("nSmoothScale 4", indent=4, endl=";\n\n") self.writeString("errorReduction 0.75", indent=4, endl=";\n\n") f.write("}\n\n") f.write("debug 0;\n\n") f.write("mergeTolerance 1E-6;\n")