Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export Assembly as TSJ #736

Closed
davidvpspace opened this issue Apr 20, 2021 · 13 comments
Closed

Export Assembly as TSJ #736

davidvpspace opened this issue Apr 20, 2021 · 13 comments
Labels
question Further information is requested

Comments

@davidvpspace
Copy link

Hi there!

I have an assembly created by multiple parts in cadquery. I would like to export it as json mesh format to later apply some textures to the different parts. With cadquery, the assembly cannot be exported directly as .tjs since only .step and .xml is available.

I have tried a workaround to convert the assembly to a single piece by and then export it to .tjs

assy = assy.toCompound() cq.exporters.export(assy, 'design1.json', tolerance=0.01, angularTolerance=0.1, exportType=exporters.ExportTypes.TJS)

However, I get the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-7f7c910c646a> in <module>
----> 1 assy.toCompound()

AttributeError: 'Assembly' object has no attribute 'toCompound'

I have installed cadquery using the master branch so I should be able to use the attribute toCompound(). Do you have an idea of what could be the source of this?

Also, is it in mind to add to cadquery a feature to specify material type to different parts of an assembly?

Thank you !

@davidvpspace davidvpspace added the question Further information is requested label Apr 20, 2021
@davidvpspace
Copy link
Author

I have also try the solution from issue #629, but I get the following error:

assy_solid = generate_solid_from_assembly(assy)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-bf842f15567a> in <module>
----> 1 assy_solid = generate_solid_from_assembly(assy)

<ipython-input-6-5f4c196566c5> in generate_solid_from_assembly(assy, fuse, name_filter_re)
      6             continue
      7 
----> 8         shape = val.obj.val()
      9 
     10         # get location

AttributeError: 'NoneType' object has no attribute 'val'

@fedorkotov
Copy link
Contributor

Please provide a minimum (non)working example including the code that creates some assembly so that we can reproduce the problem.

@Jojain
Copy link
Contributor

Jojain commented Apr 20, 2021

When did you installed cadquery ? The toCompound method has been merged yesterday, you might want to update your installation first to see if it solves the problem.

@davidvpspace
Copy link
Author

I installed it a couple weeks ago. I tried to update it this morning. Maybe I should try again, is it done by doing:?

conda install -c cadquery -c conda-forge cadquery=master

@davidvpspace
Copy link
Author

I have also try the solution from issue #629, but I get the following error:

assy_solid = generate_solid_from_assembly(assy)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-bf842f15567a> in <module>
----> 1 assy_solid = generate_solid_from_assembly(assy)

<ipython-input-6-5f4c196566c5> in generate_solid_from_assembly(assy, fuse, name_filter_re)
      6             continue
      7 
----> 8         shape = val.obj.val()
      9 
     10         # get location

AttributeError: 'NoneType' object has no attribute 'val'

I was able to solve this. I was creating an empty assembly and then adding parts. The assembly was being created properly but when I tried to run the generate_solid_from_assembly function didnt work

assy = cq.Assembly()
assy.add(part1, name="part1")
assy.add(part2, name="part2")

@fedorkotov
Copy link
Contributor

fedorkotov commented Apr 20, 2021

I was able to solve this.

I'm glad that you have managed to solve this problem yourself.

I was creating an empty assembly and then adding parts. The assembly was being created properly but when I tried to run the generate_solid_from_assembly function didnt work

Previously you posted and then deleted some huge piece of code depending on a DXF that you did not provide. Now after you have deleted that post your comment does not really explain what happened. Nobody except your knows what was inside generate_solid_from_assembly function.
Please consider adding a short summary for the benefit of others who will come to this issue from google if you have time for this.

@davidvpspace
Copy link
Author

I was able to solve this.

I'm glad that you have managed to solve this problem yourself.

I was creating an empty assembly and then adding parts. The assembly was being created properly but when I tried to run the generate_solid_from_assembly function didnt work

Previously you posted and then deleted some huge piece of code depending on a DXF that you did not provide. Now after you have deleted that post your comment does not really explain what happened. Nobody except your knows what was inside generate_solid_from_assembly function.
Please consider adding a short summary for the benefit of others who will come from google to this issue if you have time for this.

Yes, I realized that the whole code was too much for the simple question I was asking. The code inside generate_solid_from_assembly is the same as in #629

def generate_solid_from_assembly(assy, fuse=True, name_filter_re=None):
    result = []
    
    for name,val in assy.traverse():
        if name_filter_re and not re.match(name_filter_re, name):
            continue
        
        shape = val.obj.val()

        # get location
        loc = val.loc
        current = val
        while current.parent:
            loc = current.parent.loc*loc
            current = current.parent
        
        result.append(shape.located(loc))

    result = cq.Compound.makeCompound(result) 

    if fuse:
        result = result.fuse()

    return result

I was giving the function an assembly that was initially created empty and then adding the parts:

assy = cq.Assembly()
assy.add(workplane1, name="part1")
assy.add(workplane2, name="part2")
assy_solid = generate_solid_from_assembly(assy)

This was an issue as the method val() was not recognized. On the other hand if I do:

assy = cq.Assembly(workplane1, name="part1")
assy.add(workplane2, name="part2")
assy_solid = generate_solid_from_assembly(assy)

works correctly.

To close this issue, what is the command line that I should write in the conda prompt to update cadquery to include the method toCompound(). This would replace the whole function. Thx!

@fedorkotov
Copy link
Contributor

fedorkotov commented Apr 20, 2021

To close this issue, what is the command line that I should write in the conda prompt to update cadquery to include the method toCompound(). This would replace the whole function. Thx!

As @Jojain said toCompound() was merged yesterday. So you should install the latest master commit from this repo.

Maybe this is not the correct way but I usually just delete conda environment where my copy of CadQuery is installed and reinstall everything as described in CadQuery README or with mamba (see #691). This avoids all possible version conflicts and ensures latest possible versions of all dependencies are installed.

There is also conda update --all (see https://stackoverflow.com/questions/38972052/anaconda-update-all-possible-packages for example).

@jmwright
Copy link
Member

@davidvpspace You can try the following method, but what @fedorkotov has suggested about removing and re-creating the environment is the safest method. We have had multiple users end up with weird version conflicts when upgrading packages within environments.

conda remove cadquery
conda clean --all --force-pkgs-dirs
conda install --force-reinstall -c conda-forge -c cadquery=master

@adam-urbanczyk
Copy link
Member

Please wait till azure on master is green (should happen soon).

@davidvpspace
Copy link
Author

@jmwright I have done what you proposed of removing cadquery and reinstalling it, but now I get the following error. I have tried several time to delete all the files and installing it again, but the same happens:

>>> import cadquery
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\david\anaconda3\envs\cadquery\lib\site-packages\cadquery\__init__.py", line 2, in <module>
    from .occ_impl.geom import Plane, BoundBox, Vector, Matrix, Location
  File "C:\Users\david\anaconda3\envs\cadquery\lib\site-packages\cadquery\occ_impl\geom.py", line 5, in <module>
    from OCP.gp import (
ImportError: DLL load failed while importing OCP: The specified module could not be found.

@jmwright
Copy link
Member

CadQuery CI is green again, so you should be safe to try this now. Did you use the following steps to remove and recreate the environment?

conda deactivate # if you are in the cadquery environment
conda env remove -n cadquery # Should say "Removing all packages" and not give an error
conda create -n cadquery python=3.8 # I personally like to pin the version of Python
conda activate cadquery
conda install -c conda-forge -c cadquery cadquery=master

Then try import cadquery. If the steps above do not work, please post what OS you're running and what version of Anaconda/Miniconda/Mamba you are running.

@davidvpspace
Copy link
Author

Now it works! (with the steps above) Thanks for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants