Skip to content
This repository has been archived by the owner on Feb 9, 2021. It is now read-only.

Import a STEP file from the web #128

Merged
merged 5 commits into from
Dec 2, 2015
Merged

Import a STEP file from the web #128

merged 5 commits into from
Dec 2, 2015

Conversation

huskier
Copy link
Contributor

@huskier huskier commented Dec 1, 2015

This pull request adds a new function importStepFromURL(url), which will firstly download a STEP file from the web, then save the file into a OS's temp directory, finally import the STEP from the temp directory.

Now, I use the tempfile module from the python std library for the temporary file.
NOTE: the suffix=".step" parameter in the tempfile.NamedTemporaryFile() function is a must, since Part.read need the suffix parameter.

If Part.read() could support memory file reading, we do not need to save the file into disk at all. I hope CQ could support stream-like or memory-file-like file read and write for further other operations.

Here is an example in FreeCAD:

import cadquery as cq
from Helpers import show
myShape = cq.importers.importStepFromURL("https://raw.githubusercontent.com/huskier/images/master/SectionBar.step")
myShape.val().label = "SectionBar"
show(myShape)

@dcowden
Copy link
Owner

dcowden commented Dec 1, 2015

Hi, thanks for the contribution! I'll review and work on this tonight.

I would like to propose some changes. For one, I'd like to add this
functionality into the main cq package. Once it's a public function, it
should be available more easily.

As a part of adding this, a I'd also like to support (and document )
importing from the file system
On Nov 30, 2015 8:20 PM, "huskier" notifications@github.com wrote:

This pull request adds a new function importStepFromURL(url), which will
firstly download a STEP file from the web, then save the file into a OS's
temp directory, finally import the STEP from the temp directory.

By the OS's temp directory, it is the directory indicated by the TEMP
environment variable on Windows, and it is the directory "/tmp" on other
OSs. Here I assume that there is always a "/tmp" directory on unix-like
systems.

Here is an example:

import cadquery as cq
from Helpers import show
from cadquery.freecad_impl import importers
myShape = importers.importStepFromURL("https://raw.githubusercontent.com/huskier/images/master/SectionBar.step")
myShape.val().label = "SectionBar"
show(myShape)


You can view, comment on, or merge this pull request online at:

#128
Commit Summary

  • Add URL file import into importStep function, but FreeCAD has a bug
    for urllib.open() function......, not work properly....
  • Add a new function importStepFromURL(url) to import STEP file from
    the web...
  • Delete IsURL(url) function...
  • Small fixes for importStepFromURL(url) function...

File Changes

Patch Links:


Reply to this email directly or view it on GitHub
#128.

@huskier
Copy link
Contributor Author

huskier commented Dec 1, 2015

@dcowden
Importing from the file system is already implemented by @jmwright and @innovationstech.

I don't find a clean way to support importing from the web and from the file system simultaneously, and I added a new function at last.

According to the document, urllib.urlopen could support URLs and local files. If we use urlopen as the unified interface, for local files, we read, save, and again read if the memory file(OR stream file) is not supported by the Part.read() function.

My code is not perfect at all, so you can change anything as you want.

@hyOzd
Copy link
Contributor

hyOzd commented Dec 1, 2015

@huskier have you considered using python tempfile module for temporary file creation?

Also I see that you are checking for python "3"; last time I checked FreeCAD thus cadquery only worked with python 2.7. Has this changed?

@jmwright
Copy link
Collaborator

jmwright commented Dec 1, 2015

@hyOzd You are correct, FreeCAD and thus CadQuery are Python 2.7 only.

@huskier I have an example of using the tempfile module in our CadQuery module for FreeCAD. https://github.com/jmwright/cadquery-freecad-module/blob/master/CadQuery/Gui/Command.py#L162

@dcowden
Copy link
Owner

dcowden commented Dec 2, 2015

@huskier, I agree with hyOzd: we do not need the python 3 checks ( python 3x is not supported at all ), and using the tempfile module would make more sense here.

Also, and on a more 'interesting' note, lets go ahead and provide a way to access these directly off of the cadquery namespace. We need to avoid adding methods that expose the freecad_impl package.

I think we can import 'importers into the cadquery namespace init, so that we can do:

cadquery.importers.importStep...

Would you mind adding that also?

I realize that we have a lot of code floating around using freecad_impl.exporters-- i think that has the same issue. id rather those be cadquery.exporters, or, better yet, for export i'd like to provide that as a method on a cq object, so you can do:

Workplane('XY').box(1,3,3).exportStep('filename')

@dcowden
Copy link
Owner

dcowden commented Dec 2, 2015

@huskier, now that i look, we already have the necesary import in init:

from cadquery import importers

@huskier
Copy link
Contributor Author

huskier commented Dec 2, 2015

@dcowden @hyOzd @jmwright

I will look at the code, and remove the PY3 check, and use the tempfile module instead of my own way.

…pfile module from the python std library; 3. NOTE: the suffix parameter in the tempfile.NamedTemporaryFile() function is a must, since Part.read need the suffix parameter......
@huskier
Copy link
Contributor Author

huskier commented Dec 2, 2015

The updated pull request:

  1. remove the PY3 check;
  2. use tempfile module from the standard library;
    IMPORTANT NOTE: the suffix=".step" parameter in the tempfile.NamedTemporaryFile() function is a must, since Part.read need the suffix parameter.

@dcowden
Copy link
Owner

dcowden commented Dec 2, 2015

Thanks for the contribution!

dcowden added a commit that referenced this pull request Dec 2, 2015
Import a STEP file from the web
@dcowden dcowden merged commit d8d5669 into dcowden:master Dec 2, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants