Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to convert SLDPRT Solids to voxels 6

Status
Not open for further replies.

maniachalengineer

Mechanical
Dec 23, 2008
19
0
0
US
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!
 
Replies continue below

Recommended for you

I hadn't heard of Voxels till now, but of the litle I've just read, the Voxel using software would have to create the 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.
 
I do not run or own the software package that is used by my customer so I cannot fully defend the inability to read iges files. I do know that most software is written with specific goals and specifications- many, many 3D design systems were written that do not use nor even acknowledge formats like Iges, step, or stl.
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.
 
One of the secrets to doing real time 2D graphics with small computers is to get the problem into pixel space as soon as possible, so all of the math associated with the image can be done with integers. E.g. Bresenham's algorithm instead of trig.
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
 
Here's an interesting little macro to voxelize a solid body.
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 VOXSIZE As Double = 0.01
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)
 
Mike Halloran- I really appreciate your insights and replies- these look genuinely helpful. You seem to have a good understanding of the underlying conundrum with file formats that do not easily "bridge" the CAD world to the Medical files...
I will see if I can find a "neutral" format that is acceptable to the customer's needs and helps define a mutual "demarcation".
 
Handleman:

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!
 
Handleman:

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
 
Based on my understanding, voxel is to 3d CAD as TIFF file is to DXF file (one is mathematical and the other is discrete pixel data). You are looking at "3d" pixels because it's one of the few ways to work with highly varied data in 3d, as opposed to parts made of 100% the same material. In fact, computer games used this for 3d graphics before hardware 3d processing was common.

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.

The biggest detail is to understand the format that is expected, since I suspect it may be specific to each system out there.
 
Maniacal, a cube is exactly the wrong example to feed to Handleman's code. If I'm understanding it correctly, it will just subdivide the original cube into a bunch of smaller cubes, all adjacent and visually indistinct.

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
 
To use it, go to Tools->Macro->New. Make sure "SolidWorks VBA Macro" is selected from the popup's dropdown and type in a name for the new macro. The correct extension (.swp) will be added automatically. Once you accept the name, you should be presented with a code editing window containing a couple of lines of code. Completely delete those code lines and paste in the macro text, then save. Then you can run the macro. You should modify the constant for voxel size based on your model size. 0.001m (1mm) voxels would be ridiculous for a model on the order of a meter or so.

-handleman, CSWP (The new, easy test)
 
What you really need more than anything else is communication. For you customer to simply say "send me voxels" is absurd. What was your customer paying for? If they needed voxels, why was the work done in SW?

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.
 
keepinitcool and Milehalloran;

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!
 
This thread has definately piqued my interest in the voxel format, as I can see the real world applications for it.

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.

 
 http://www.grahamwideman.com/
Cpretty;
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.
 
Status
Not open for further replies.
Back
Top