Need to convert SLDPRT Solids to voxels
Need to convert SLDPRT Solids to voxels
(OP)
Has anybody worked with a program that conceivably would take a solid model (organic shapes with complex surfaces) from Solidworks and output or convert to voxels? I have a customer who has some very sophisticated simulation software that relies on voxel input. My goal is to re-define the separate models into 1mm cubes or "voxels".
From some recent reading I've deduced that voxels are "cubes" that have a number of associated properties for each cube- thus enabling things like anisotropy or optical properties, even time constraints on each cube or groups of cubes. Anyway, I assume there must be a pathway to render SLDPRT files into "slices" or other intermediate topologies that may be a step towards voxels. I've spent more than a day so far looking online for solutions to no avail. Any ideas?
Thanks in advance!
From some recent reading I've deduced that voxels are "cubes" that have a number of associated properties for each cube- thus enabling things like anisotropy or optical properties, even time constraints on each cube or groups of cubes. Anyway, I assume there must be a pathway to render SLDPRT files into "slices" or other intermediate topologies that may be a step towards voxels. I've spent more than a day so far looking online for solutions to no avail. Any ideas?
Thanks in advance!






RE: Need to convert SLDPRT Solids to voxels
Rob Stupplebeen
RE: Need to convert SLDPRT Solids to voxels
SolidWorks does not have an export function for this. A model could be sliced up into cubes, creating a multi-body part, but I don't know if the Voxel software would recognise that.
RE: Need to convert SLDPRT Solids to voxels
Thanks for the suggestion- I have included neutral formats in my searches so far, I also suspect these will be much more likely to meet with success.
RE: Need to convert SLDPRT Solids to voxels
RE: Need to convert SLDPRT Solids to voxels
The "heritage" of the "voxel software" being used is in the medical field- using "sliced data" files orginally generated from MRI scans. The colors for various organs and tissues are then assigned to a discrete "pallette" and the similar colors on adjacent layers (slices) are combined and cut into small cubes or "voxels". Apprently this technique has been used for some time quite successfully to generate very accurate shapes of internal organs. Obviously the original goals did not include mechanical CAD software or formats. Now we are providing additional capability for the system that utilizes 3D CAD models of organs (SolidWorks) to accomplish some unique molding and fabrication. These same CAD models can be very useful as simulation input to the medical types ONLY if we can arrange it in voxels for input to their system. I deliberately ommited these details in the original post but perhaps it is useful in some way. I would describe it as evolving and merging disparate systems.
RE: Need to convert SLDPRT Solids to voxels
RE: Need to convert SLDPRT Solids to voxels
I'm guessing that voxels are used for similar reasons; not surprising given the amount of data to be stored and presented.
When the issue was first raised, I was thinking it should be a simple matter to 'voxelize' a DXF file, just by rounding all the float numbers to a chosen voxel size, a task that could be done by unix style filters... but it would still be a DXF file.
What is surprising is that there seems to be zero overlap in file formats. I.e., while I got many hits on file formats for voxels, there doesn't appear to _be_ a neutral file format that's used in both CAD and medical imaging universes.
Maniacal, the useful neutral CAD file formats have been documented formally, and are available to the public.
Can your customer name a similarly documented and disclosed file format for voxels? Or has he otherwise disclosed in what form the data is to arrive at his demarcation point?
Mike Halloran
Pembroke Pines, FL, USA
RE: Need to convert SLDPRT Solids to voxels
It will create individual voxel bodies for an entire part. The voxel size and fill percentage are constants at the top.
Basically, the macro will create a cubical body for each cube of the part that is filled to at least the fill percentage.
Note that voxel size is in METERS, and the fill percentage is not really percentage. 0.5 means 50%.
This will take a long time to run if you make the voxel size very small compared to the model size.
I don't particularly think it's actually very useful, but I've never seen a macro yet that takes pure math and makes dumb geometry out of it, so I thought I'd give it a shot.
CODE
Const FillPct As Double = 0.5
Dim swApp As SldWorks.SldWorks
Dim swMainBody As Body2
Dim allBodies As Variant
Dim TmpBod As Body2
Dim swTool As Body2
Dim swMod As Modeler
Dim myBox(8) As Double
Dim swPart As PartDoc
Dim swDoc As SldWorks.ModelDoc2
Dim NewBod As Body2
Dim NewBods As Variant
Dim BodyVol As Double
Dim BodyProps As Variant
Dim myFeat As Feature
Dim myCorners As Variant
Sub main()
Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
Set swPart = swDoc
allBodies = swPart.GetBodies2(swSolidBody, False)
Set swMainBody = allBodies(0)
Set swMod = swApp.GetModeler
myCorners = swMainBody.GetBodyBox
If myCorners(0) > myCorners(3) Then
myCorners(0) = (CInt(myCorners(0) / VOXSIZE) + 1) * VOXSIZE
myCorners(3) = (CInt(myCorners(3) / VOXSIZE) - 1) * VOXSIZE
Else
myCorners(0) = (CInt(myCorners(0) / VOXSIZE) - 1) * VOXSIZE
myCorners(3) = (CInt(myCorners(3) / VOXSIZE) + 1) * VOXSIZE
End If
If myCorners(1) > myCorners(4) Then
myCorners(1) = (CInt(myCorners(1) / VOXSIZE) + 1) * VOXSIZE
myCorners(4) = (CInt(myCorners(4) / VOXSIZE) - 1) * VOXSIZE
Else
myCorners(1) = (CInt(myCorners(1) / VOXSIZE) - 1) * VOXSIZE
myCorners(4) = (CInt(myCorners(4) / VOXSIZE) + 1) * VOXSIZE
End If
If myCorners(2) > myCorners(5) Then
myCorners(2) = (CInt(myCorners(2) / VOXSIZE) + 1) * VOXSIZE
myCorners(5) = (CInt(myCorners(5) / VOXSIZE) - 1) * VOXSIZE
Else
myCorners(2) = (CInt(myCorners(2) / VOXSIZE) - 1) * VOXSIZE
myCorners(5) = (CInt(myCorners(5) / VOXSIZE) + 1) * VOXSIZE
End If
Dim i As Double
Dim j As Double
Dim k As Double
For i = 0 To UBound(myCorners)
Debug.Print myCorners(i)
Next i
For i = myCorners(0) To myCorners(3) Step VOXSIZE
For j = myCorners(1) To myCorners(4) Step VOXSIZE
For k = myCorners(2) To myCorners(5) Step VOXSIZE
myBox(0) = i 'x
myBox(1) = j 'y
myBox(2) = k 'z
myBox(3) = 0 'x
myBox(4) = 0 'y
myBox(5) = 1 'z
myBox(6) = VOXSIZE 'width
myBox(7) = VOXSIZE 'length
myBox(8) = VOXSIZE 'height
Set swTool = swMod.CreateBodyFromBox(myBox)
Set TmpBod = swMainBody.Copy
NewBods = TmpBod.Operations2(SWBODYINTERSECT, swTool, Empty)
Set TmpBod = Nothing
Set swTool = Nothing
If IsArray(NewBods) Then
If Not (NewBods(0) Is Nothing) Then
Set NewBod = NewBods(0)
BodyProps = NewBod.GetMassProperties(0.05)
BodyVol = BodyProps(3)
If BodyVol > (FillPct * (VOXSIZE ^ 3)) Then
Set NewBod = swMod.CreateBodyFromBox(myBox)
NewBod.CreateBaseFeature NewBod
Set NewBod = Nothing
End If
Debug.Print i, j, k
End If
End If
Next k
Next j
Next i
End Sub
-handleman, CSWP (The new, easy test)
RE: Need to convert SLDPRT Solids to voxels
I will see if I can find a "neutral" format that is acceptable to the customer's needs and helps define a mutual "demarcation".
RE: Need to convert SLDPRT Solids to voxels
WOW! Your reply may be exactly what I need if I can figure how to use it. Thank you very much for the effort and interest you've shown!
Can you give me an idea of how this can be used? I need to know the input data format, how to "execute" the file, etc...
Thank You!
RE: Need to convert SLDPRT Solids to voxels
I copied and pasted the script- assuming it is a macro to run in Soliworks, renamed the extension as .swb.
I then tried running the "macro" on a simple cube about 1in x 1in x .4in, it appeared to run (no errors) but where does it put the results? I cannot see any changes to the actual part or any new file that may have been created...
thx in advance
RE: Need to convert SLDPRT Solids to voxels
This article talks about going from MRI data to CAD data, so you are looking to go the other direction. So apparently tools exists for at least one direction.
htt
The biggest detail is to understand the format that is expected, since I suspect it may be specific to each system out there.
RE: Need to convert SLDPRT Solids to voxels
It can import 3D cad format and convert it to its native format then to a voxel format.
http://www.whiterockscience.com/moritz/voxel.html
RE: Need to convert SLDPRT Solids to voxels
Better to start with a cylinder or a sphere.
But you've then still got a Solidworks file, not a voxel file... but keepin may have solved that problem.
Mike Halloran
Pembroke Pines, FL, USA
RE: Need to convert SLDPRT Solids to voxels
-handleman, CSWP (The new, easy test)
RE: Need to convert SLDPRT Solids to voxels
The voxel model request could be coming from anywhere, most likely a non-techie who only knows what he's been told to ask for. What you need to do is get on the phone with the person who actually runs the software and find out what they might be able to use.
Get a list of formats their software can import, and compare it to what SW can export. You may need to try several different formats before finding a winner.
RE: Need to convert SLDPRT Solids to voxels
Thanks again for the great suggestions, code, and links- I will work on this later in the week as an opportunity to learn more about voxels and possibly provide a solution. Seems that voxels are one of the few formats that enable highly detailed characteristics in a 3D model to accurately convey anisotropy, non-homogeneity, temporal assets, etc.. I'm taking full scale CAD models of organs (approx 100-400mm typical dimensions) and dividing down to 1mm voxels- should be reasonable.
I'm attempting this particular solution for my customer freely- I have separately provided several related CAD organic models and molded parts that will benefit from the voxel code when done.
Outstanding, much appreciated! This hopefully will benefit many people in the longer run!
RE: Need to convert SLDPRT Solids to voxels
Converting between formats should be acheivable once the syntax issue is sorted out. Looks like the voxel format is a bit of a nightmare though, with every viewer etc having their own file format.
Voxel to SW looks like the creation of a number of cubes at specified positions. From what I can find, traversing through the voxel file and deciding whether a voxel is to be displayed solid or not is all that should be needed.
SW to voxel is likely to be very similar to handleman's code, but instead of creating cubes, it is about deciding whether the cube has enough volume to be considered a full voxel.
After a bit of a search for more info, I came across the following site. The first links may be of use to those trying to have a play with these.
RE: Need to convert SLDPRT Solids to voxels
Thanks for the link to lots of interesting info related to voxels- my initial foray into the sites indicates this should prove useful in several ways. I also agree with your comments about the boundary definition for the voxels, this should be done in a very coordinated manner. In my understanding so far, if one has a single, homogenous, arbitrarily shaped "organic" solid it can be subdivided into voxels but the outer surface(s) boundary is typically done using some type of threshold or averaging scheme for the outermost voxels. Unfortunately this may not be the optimal plan for adjacent solids.
As a simplified organ pair example, the heart and lungs are very close physically, so if both are "voxelized" simultaneously (resulting in two separate "voxel solids", the surface(s) boundary voxels will be significantly different than when a single organ is voxelized (i.e. heart OR lungs separately and independently). The "multi-solid" strategy affects the tiny "airspaces" between the adjacent organs in significant ways and it is typically important to minimize "air" where it does not actually exist in various types of simulations.
RE: Need to convert SLDPRT Solids to voxels
RE: Need to convert SLDPRT Solids to voxels
Multibody parts don't even perform as well as assemblies.
-handleman, CSWP (The new, easy test)
RE: Need to convert SLDPRT Solids to voxels
-handleman, CSWP (The new, easy test)
RE: Need to convert SLDPRT Solids to voxels
I expect that a cloud of points at the centers of the cubes that you are currently creating would be as useful as an array of cubes as a starting point for voxelization.
Eric
RE: Need to convert SLDPRT Solids to voxels
HOLY COW! The macro you provided works really well! I ran it on a 0.5in dia hemisphere with 1mm voxels and 50% fill settings, the results are shown in the attached pictorial.The 50% setting appears to produce a "3D circumscribed" voxel arrangement. This is a great start towards my goals on voxels! The macro ran about 2.1 secs generating the voxels, and about 4secs creating the assembly with 798 voxels. When I created a more complex "organic" part measuring about 5in x 2in x 1in and asked for 1mm voxels it generated about 75,000 voxels in about 5 minutes and I stopped the assembly sequence after 30 minutes. It was obviously struggling with the task too much. But I believe this has convinced me that there is a decent solution using this or a similar strategy. I've asked my customer to provide a sample format for the voxels he can use, I'll try to proceed from there. I really appreciate this invaluable and enlightening glimpse into the macros with SolidWorks- I'd never used macros with SW before and it is awesome! Thank you very much!!
RE: Need to convert SLDPRT Solids to voxels
Not a huge one, but I never reset the body volume summation to zero. Meaning that once it found one voxel that was full enough, the percentage was essentially anything over zero for all subsequent voxels.
Attached are two versions, one that's similar to the earlier posted one except that you can set a max number of voxels per assembly. Once that number is reached, the assembly is saved, closed, and another picks up where that one left off. Once the macro finishes, you can easily assemble the different subassemblies into one main assembly. If SW will do it, that is.
The other version incorporates Eric's suggestion of a 3D sketch point cloud. It's much simpler.
http:/
http:/
-handleman, CSWP (The new, easy test)
RE: Need to convert SLDPRT Solids to voxels
You are really outstanding! I certainly appreciate your creative and useful macros. I'm definitely looking forward to exercising these newer versions to see how they work. I also enjoy "dissecting" these macros so I can use them as a teaching tool for myself. Truly awesome!
Thank you very much!
RE: Need to convert SLDPRT Solids to voxels
Maybe it works with opencascade. Have a look at the documentation I attached.
May I ask for the name of this "sophisticated simulation software that relies on voxel input" ???
RE: Need to convert SLDPRT Solids to voxels
Also blender 3d has a developer designing a plugin or script for future addition to its core functions.
it is still very much at alpha testing stage but could be a valuable conversion tool for you later on save sw part out as as iges or sat and import it in to blender and save out as voxel format .
just something to keep an eye on
Cheers
RE: Need to convert SLDPRT Solids to voxels
CSWP-Surf
RE: Need to convert SLDPRT Solids to voxels
The information gathered from this thread has been very useful for my goal of converting Solidworks models to Voxels. The primary work I'm doing now is learning to write and adapt some of the initial Solidworks macro conversion programs provided by Handleman in this thread... This has opened my eyes to a whole new world of powerful functionality within SolidWorks that I'd never used before. I was unsure about whether the initial thread was more likely to solicit meaningful replies in the computer programming forums or the Solidworks forum (I actually double posted the intial query- a no-no)- now I'm glad that I chose the SolidWorks forum because I'll use these newfound skills on many varied problems in SolidWorks!