Skip to main content

Import CAD drawings into Django and inspect them in VR with A-Frame

Project description

Import CAD drawings into Django and inspect them in VR with A-Frame

Requirements

This project is tested on Django 5.1.2 and Python 3.12. It heavily relies on outstanding ezdxf for handling DXF files, django-colorfield for admin color fields.

Installation from PyPI

Activate your virtual environment and install with:

python -m pip install django-cad-inspector

In your Django project add:

INSTALLED_APPS = [
    # ...
    "colorfield",
    "cadinspector",
]
# my_project/urls.py
urlpatterns = [
    # ...
    path('3d/', include('cadinspector.urls', namespace = 'cadinspector')),
]

Add two lists to my_project/settings.py:

CAD_LAYER_BLACKLIST = [...]
CAD_BLOCK_BLACKLIST = [...]

Here you can store names of layers and blocks you don’t want to be processed. Migrate and create a superuser.

Templates

You also need a base.html template with following template blocks (a sample base.html is provided among package templates).

{% block extra-head %}
{% end block extra-head %}
...
{% block content %}
{% endblock content %}

Package comes with four templates, cadinspector/entity_list.html, cadinspector/entity_detail.html, cadinspector/scene_list.html and cadinspector/scene_detail.html. Copy and override them in your project templates if you want to add your styles.

Usage

Run the server and navigate to http://127.0.0.1:8000/3d: you will be presented with a Scene list. Of course there still are no scenes, so clik on the details and on the Add Scene link. Enter your credentials to access the admin site.

Scenes from a DXF

Enter title, description and eventually an Equirectangular image to simulate the environment, then upload a DXF file (it is a CAD exchange file). The DXF file must contain some meshes (if you have 3DSolids you have to convert them to Meshes). Click on the Save and continue button. Thanks to the outstanding ezdxf library, meshes are converted to *.obj files, incorporated into Entity models and associated to the Scene via Staging inlines. Each Staging holds information for position, rotation, scale, color (extracted from the CAD Layer the mesh belonged to) and some data (more on that later). WARNING: updating the DXF file will remove all entities staged on the Scene, but not the entities. Also CAD Blocks with meshes will be imported, each Block will be transformed into an Entity, while Insertions will be transformed into Stagings and associated to the Scene. In the case of Blocks, the appended data will contain also Block attributes. WARNING, some restrictions occour for insertions when pitch rotation is 90 or -90 degrees. Visit the site at http://127.0.0.1:8000/3d again. Finally the Scene is listed. Click on the link and access the A-Frame virtual reality: hold down the right mouse button to look around, and use WASD keys to move. When the cursor moves on an object, a popup with data should appear (if data is associated to the staged entity).

Entities

You can create Entities navigating to http://127.0.0.1:8000/admin/cadinspector/entity/add/: enter a Title and Description, then upload an *.obj file. If provided, the associated *.mtl file and eventual images. Check the Switch field if your object was created in CAD: A-Frame coordinate system is rotated with respect to CAD coordinate system. Alternatively you can upload a *.gltf file, which is the recommended format in A-Frame. If uploaded, all other formats will be ignored.

Add Entities to Scenes

In http://127.0.0.1:8000/admin/cadinspector/scene/ choose a Scene to update. Add a Staged entity, select one of the Entities you created previously, adjust color, position, rotation and scale. Stage as many entities you want (even multiple specimens of the same entity), then update the Scene.

Entity utilities

Navigating to http://127.0.0.1:8000/admin/cadinspector/entity/ shows list of existing Entities. Two admin actions are implemented to manage them:

Check file names

Uploading *.mtl file and images in Django may change their filename, i.e. to avoid duplicate filenames. This can lead *.obj files and *.mtl files to miss their assets (filenames are hardcoded within these files). Select corrupted Entities and run the routine: hardcoded filenames will be rewritten to match actual uploaded filenames.

Delete unstaged entities

As seen before, deleting a Staging does not delete the corresponding Entity, which can be staged in multiple Scenes. Select all Entities and run this routine if you want to get rid of unstaged ones.

A-Frame Visual Inspector

Once in the A-Frame window, if you press Ctrl + Alt + i you will open the A-Frame Visual Inspector. It’s possible to modify objects in the Inspector, save a *.gltf file from the whole scene, and then add it to an Entity.

Next steps

Create entities with lights, add shadows and some physics.

Tests

Testing is done with unittest. At the moment coverage is 97%. Tested for Django 4.2 against Python 3.9, 3.10, 3.11, 3.12 versions and for Django 5.1 against Python 3.10, 3.11, 3.12 versions (3.13 on Windows).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_cad_inspector-0.3.0.tar.gz (17.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_cad_inspector-0.3.0-py3-none-any.whl (21.8 kB view details)

Uploaded Python 3

File details

Details for the file django_cad_inspector-0.3.0.tar.gz.

File metadata

  • Download URL: django_cad_inspector-0.3.0.tar.gz
  • Upload date:
  • Size: 17.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for django_cad_inspector-0.3.0.tar.gz
Algorithm Hash digest
SHA256 389fcbf591a84d7eb2a865614cdbba6bc2c300c8c6598d7e8db722f9ecb43cc3
MD5 fef44a31019c7fd49176c8d931da0498
BLAKE2b-256 02141da09ef7913f091f19b8b163959518471b152fa94c5985c083040a93b380

See more details on using hashes here.

File details

Details for the file django_cad_inspector-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_cad_inspector-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e20900e4ec4b21e69533362101d33495d0bcf33e5afeb3e1eb120995f4afbc02
MD5 53927991ffc94c1bfadba74d1829c139
BLAKE2b-256 e974ab325f4265e08a7a955e8c2f1a8c7dcab915051d9dc5d00cd7b49ac38372

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page