feature request: Viewing and editing OpenFOAM blockMeshDict in FreeCAD?

A subforum specific to the development of the OpenFoam-based workbenches ( Cfd https://github.com/qingfengxia/Cfd and CfdOF https://github.com/jaheyns/CfdOF )

Moderator: oliveroxtoby

foadsf
Posts: 30
Joined: Fri Mar 06, 2015 10:02 pm

feature request: Viewing and editing OpenFOAM blockMeshDict in FreeCAD?

Postby foadsf » Tue Aug 21, 2018 12:07 pm

Hello Everyone,

Following this reddit post I was advised to ask feature request here in the forum.
It would be great if we could have a plugin like CadQuery's module where on one panel we could edit the blockMeshDict source code and then update the view. And there could be a live preview option to render when an edit is applied. Also some menu options to add point, line, circle... like the ones in Gmsh GUI.

Best,
Foad
User avatar
bernd
Posts: 8046
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: feature request: Viewing and editing OpenFOAM blockMeshDict in FreeCAD?

Postby bernd » Tue Aug 21, 2018 8:03 pm

since I even not know what is the feature request about I move it to cfd. May be the guys there know more about this.

bernd
foadsf
Posts: 30
Joined: Fri Mar 06, 2015 10:02 pm

Re: feature request: Viewing and editing OpenFOAM blockMeshDict in FreeCAD?

Postby foadsf » Wed Aug 22, 2018 6:22 am

Thanks. I did not know about the CFD section. The feature I'm requesting is to import and modify OpenFOAM's blockMeshDict as explained here.
foadsf
Posts: 30
Joined: Fri Mar 06, 2015 10:02 pm

Re: feature request: Viewing and editing OpenFOAM blockMeshDict in FreeCAD?

Postby foadsf » Wed Aug 22, 2018 9:16 am

First step we can implement a simple Python 2.7 script which reads the blockMeshDict file, looks for vertices keyword (which shouldn't be after // or in between /* ... */) and reads the list of tuples (x,y,z) in the parentheses after the keyword. Then creates points, for example in Draft workbench, labeling them consequently. The last part I'm trying to implement here.
User avatar
bernd
Posts: 8046
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: feature request: Viewing and editing OpenFOAM blockMeshDict in FreeCAD?

Postby bernd » Wed Aug 22, 2018 10:45 am

why not import it as a FEM mesh object? We have some mesh importer already. Have a look here: https://github.com/FreeCAD/FreeCAD/tree ... m/feminout The point numbers could be drawn with Draft anyway.
foadsf
Posts: 30
Joined: Fri Mar 06, 2015 10:02 pm

Re: feature request: Viewing and editing OpenFOAM blockMeshDict in FreeCAD?

Postby foadsf » Wed Aug 22, 2018 11:03 am

There is no mesh yet. The issue is that usually blockMesh is not able to compile the blockMeshDict files. visualisation helps to see the problems.

So far I'm able to read the blockMeshDict file by:

Code: Select all

import os
os.chdir(os.path.dirname(__file__))
with open("blockMeshDict", "r") as f:
	s=f.read()
now I'm trying to print out the content in the parentheses in the line after vertices keyword with:

Code: Select all

import re
r1=re.search(r'vertices\n\((.*?)\)', s)
print r1.group(1)
But it does not work! I posted the problem here in StackOverflow. Hopefully I can solve this.
User avatar
oliveroxtoby
Posts: 279
Joined: Fri Dec 23, 2016 9:43 am
Location: South Africa

Re: feature request: Viewing and editing OpenFOAM blockMeshDict in FreeCAD?

Postby oliveroxtoby » Wed Aug 22, 2018 12:18 pm

foadsf wrote:
Wed Aug 22, 2018 6:22 am
Thanks. I did not know about the CFD section. The feature I'm requesting is to import and modify OpenFOAM's blockMeshDict as explained here.
That would be cool, but I think it's unlikely that I or other current developers would feel it is a priority. But if you want to have a crack at it and issue us a pull request, that would be great!
foadsf
Posts: 30
Joined: Fri Mar 06, 2015 10:02 pm

Re: feature request: Viewing and editing OpenFOAM blockMeshDict in FreeCAD?

Postby foadsf » Wed Aug 22, 2018 12:25 pm

happy to see that you liked the idea.
I'm trying, but I'm no expert! a little bit of help here and here would be highly appreciated.
foadsf
Posts: 30
Joined: Fri Mar 06, 2015 10:02 pm

Re: feature request: Viewing and editing OpenFOAM blockMeshDict in FreeCAD?

Postby foadsf » Wed Aug 22, 2018 1:33 pm

thanks to this post I'm now able to import the vertices as tuples with

Code: Select all

r1 = re.search(r'vertices\s*\(\s*(.*)\s*\)', s, re.DOTALL)
vertices = [(float(v[0]),float(v[1]),float(v[2]))
        for v in re.findall(r'\(\s*([0-9.]+)\s+([0-9.]+)\s+([0-9.]+)\s*\)', r1.group(1))]

print(vertices)
foadsf
Posts: 30
Joined: Fri Mar 06, 2015 10:02 pm

Re: feature request: Viewing and editing OpenFOAM blockMeshDict in FreeCAD?

Postby foadsf » Wed Aug 22, 2018 3:56 pm

With the help of other guys I made this script which imports the vertices from a blockMeshDict file and displays them:

Code: Select all


#the script must be in the same folder as blockMeshDict file

#changing the active directory to the location of script
import os
os.chdir(os.path.dirname(__file__))

#read the file and import it as a string
with open("blockMeshDict", "r") as f:
	s=f.read()
  
import re

#clean up the C/C++ comments
def stripcomments(text):
    return re.sub('//.*?(\r\n?|\n)|/\*.*?\*/', '', text, flags=re.S)
s=stripcomments(s)

#extract the vertices into a list of tuples
r1 = re.search(r'vertices\s*\(\s*(.*)\s*\)', s, re.DOTALL)
vertices = [(float(v[0]),float(v[1]),float(v[2]))
        for v in re.findall(r'\(\s*([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s*\)', r1.group(1))]

#display the point and labels
App.newDocument("step")
Gui.activateWorkbench("DraftWorkbench")
import Draft

for vertexNum, vertex in enumerate(vertices):
  p=Draft.makePoint(vertex[0],vertex[1],vertex[2])
	p.Label=str(vertexNum)
	Draft.makeText([str(vertexNum)],point=FreeCAD.Vector(vertex[0],vertex[1],vertex[2]))

I will keep follow this issue on this GitHub Gist