Skip to content

Commit

Permalink
Made assets optional and created default README.md.
Browse files Browse the repository at this point in the history
  • Loading branch information
afshawnlotfi committed Jul 23, 2024
1 parent f8f05f2 commit 2af2ce6
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 21 deletions.
4 changes: 2 additions & 2 deletions examples/assembly.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -69,7 +69,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
Expand Down
5 changes: 3 additions & 2 deletions orion_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def cli():
@click.option("--name", help="The name of the project", required=False)
@click.option("--cad_path", help="The path for a step file (CAD/3D) to be processed with the tool", type=click.Path(), required=False)
@click.option("--remote_url", help="The URL of the remote repository", required=False, default=None)
def create_command(name: str, cad_path: str, remote_url: Optional[str]):
@click.option("--include_assets", help="Include assets in the project", is_flag=True)
def create_command(name: str, cad_path: str, remote_url: Optional[str], include_assets: bool):
"""Create a new project"""
from pathlib import Path
from orion_cli.services.create_service import CreateService
Expand Down Expand Up @@ -65,7 +66,7 @@ def create_command(name: str, cad_path: str, remote_url: Optional[str]):
# Create the project
service = CreateService()
try:
service.create(name, project_path, cad_path, remote_url)
service.create(name, project_path, cad_path, remote_url, include_assets)
logger.info(f"Project '{name}' has been created/updated at {project_path / name}")
logger.info(f"Original CAD file: {cad_path}")
logger.info(f"CAD file has been copied in the project directory.")
Expand Down
32 changes: 23 additions & 9 deletions orion_cli/services/cad_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class InventoryItem(BaseModel):
name: str
price: Optional[float] = None

MAIN_ASSEMBLY_NAME = "MAIN_ASM"

@dataclass
class Inventory:
Expand Down Expand Up @@ -92,6 +93,7 @@ class ProjectOptions(BaseModel):
max_name_depth: int = 3
normalize_axis: bool = False
use_references: bool = True
include_assets: bool = False

@dataclass
class Project:
Expand Down Expand Up @@ -189,6 +191,7 @@ def read_cqassembly(
@staticmethod
def assign_unique_part_names(part_ref: PartRef, project: Project, index: AssemblyIndex):
part_name = part_ref.name
assert part_name != MAIN_ASSEMBLY_NAME, f"part name {part_name} is reserved for main assembly"
# check if part name already exists
if part_name in index.part_names:
prev_part_ref = index.part_names[part_name]
Expand Down Expand Up @@ -352,17 +355,28 @@ def write_project(project_path: Union[Path, str], project: Project, index: Optio
logger.info(f"- Exported part '{part_name}'")

# Generate SVGs for each part if they are modified or don't exist
svg_path = assets_path / f"{part_name}.svg"
if not index or index and checksum in index.is_part_modified or not svg_path.exists():
logger.info(f"- Generating SVG for part '{part_name}'")
svg = getSVG(part, {"showAxes": False, "marginLeft": 20})
with open(svg_path, "w") as f:
if project.options.include_assets:
svg_path = assets_path / f"{part_name}.svg"
if not index or index and checksum in index.is_part_modified or not svg_path.exists():
logger.info(f"- Generating SVG for part '{part_name}'")
svg = getSVG(part, {"showAxes": False, "marginLeft": 20})
with open(svg_path, "w") as f:
f.write(svg)

if project.options.include_assets:
logger.info("- Generating SVG for main assembly, this might take a sec ...")
if index is None or project.root_assembly.path in index.is_assembly_modified:
cq_assembly = project.root_assembly.to_cq(project)
cq_comp = cq_assembly.toCompound()
svg = getSVG(cq_comp, {"showAxes": False, "marginLeft": 20})
with open(assets_path / f"{MAIN_ASSEMBLY_NAME}.svg", "w") as f:
f.write(svg)

# Remove any svg files that are not in the inventory (removed)
for svg_path in assets_path.glob("*.svg"):
if svg_path.stem not in part_names:
svg_path.unlink()
logger.info("- Removing SVG files not in inventory")
for svg_path in assets_path.glob("*.svg"):
if svg_path.stem not in part_names and svg_path.stem != MAIN_ASSEMBLY_NAME:
svg_path.unlink()


with open(inventory_path / "README.md", "w") as f:
f.write(CadService.inventory_markdown(project.inventory, assets_path.relative_to(project_path)))
Expand Down
15 changes: 8 additions & 7 deletions orion_cli/services/create_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import yaml
import shutil

from orion_cli.services.cad_service import CadService, ProjectOptions
from orion_cli.services.cad_service import CadService, ProjectOptions, MAIN_ASSEMBLY_NAME
from orion_cli.helpers.config_helper import ProjectConfig
from orion_cli.templates.README_template import README_TEMPLATE
from orion_cli.templates.gitignore_template import GITIGNORE_TEMPLATE
from .base_service import BaseService

class CreateService(BaseService):
def create(self, name: str, path: Union[str, Path], cad_path: Union[str, Path], remote_url: Optional[str] = None):
def create(self, name: str, path: Union[str, Path], cad_path: Union[str, Path], remote_url: Optional[str] = None, include_assets: bool = True):
"""Create a new project"""

project_path = Path(path) / name
Expand All @@ -23,7 +25,7 @@ def create(self, name: str, path: Union[str, Path], cad_path: Union[str, Path],
CadService.create_project(
project_path=project_path,
cad_file=cad_path,
project_options=ProjectOptions(),
project_options=ProjectOptions(include_assets=include_assets),
verbose=True
)

Expand All @@ -50,14 +52,13 @@ def create(self, name: str, path: Union[str, Path], cad_path: Union[str, Path],
# Initialize a new Git repository
subprocess.run(["git", "init", "--initial-branch=main"], cwd=project_path, check=True)
# Path to the template .gitignore file
template_gitignore_path = Path(__file__).resolve().parent.parent / 'templates' / 'gitignore_template'

# Read the content of the template .gitignore file
gitignore_content = template_gitignore_path.read_text()

gitignore_content = GITIGNORE_TEMPLATE
readme_content = README_TEMPLATE(name, MAIN_ASSEMBLY_NAME)
# Write the content to the new project's .gitignore file
(project_path / ".gitignore").write_text(gitignore_content)

(project_path / "README.md").write_text(readme_content)
click.echo("Git repository initialized and .gitignore file created.")

# Make initial commit
Expand Down
5 changes: 5 additions & 0 deletions orion_cli/templates/README_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
README_TEMPLATE = lambda NAME, ASSEMBLY_PATH: f"""
# {NAME}
![{ASSEMBLY_PATH}]({ASSEMBLY_PATH})
"""
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
GITIGNORE_TEMPLATE = """
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
.orion_cache
Expand Down Expand Up @@ -221,4 +222,5 @@
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
.apdisk
"""

0 comments on commit 2af2ce6

Please sign in to comment.