Skip to content

Commit

Permalink
Merge pull request #20 from ldevillez/Properties_module
Browse files Browse the repository at this point in the history
Properties module
  • Loading branch information
ldevillez authored Sep 23, 2024
2 parents ae0255b + 3c8f630 commit 8f834d9
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 13 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# Changelog

## v0.7.2 - 2024/09/19
## [Unreleased]
### Added
- Adding properties module
### Changed
### Deprecated
### Removed
### Fixed
### Security

## [Unreleased]
## v0.7.2 - 2024/09/19
### Added
- Adding the type of component into the display of the stat module
- Adding a flag to filter the type of components
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,14 @@ This tool help you clean the directory of your project and remove unused files
```
pyswtools clean PATH/TO/DIR/PROJECT/ PATH/TO/MAIN/ASSEMBLY
```
It will output all the solidworks files in `PATH/TO/DIR/PROJECT/` that are not used in the assembly.
It will output all the solidworks files in `PATH/TO/DIR/PROJECT/` that are not used in the assembly.

### Properties
This tool help you duplicate properties from a template to files

#### How to use

```
pyswtools properties PATH/TO/DIR/TEMPLATE_PART PATH/TO/FILE
```
It will add to `PATH/TO/FILE` the properties of `PATH/TO/DIR/TEMPLATE_PART`. If the target is a file, it will add the properties. If it is an assembly it will apply it to all its children instead.
27 changes: 18 additions & 9 deletions pyswtools/helper_sw.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,27 @@ def open_app_and_file(path_file: str):
sw_app = open_app()

# Open the file
sw_doc = sw_app.OpenDoc6(os.path.abspath(path_file), 2, 1, "", VT_BYREF, VT_BYREF)

# Get the filename
_, filename = os.path.split(path_file)

# Activate it
sw_app.ActivateDoc3(filename, True, 2, VT_BYREF)
sw_doc, filename = open_file(sw_app, path_file)

return sw_app, sw_doc, filename


def open_drawing(sw_app, path_file: str):
def open_file(sw_app, path_file: str):
"""
Create open document activate it
"""
# Open the assembly
sw_doc = sw_app.OpenDoc6(os.path.abspath(path_file), 3, 1, "", VT_BYREF, VT_BYREF)

# Default file type is part
type_file = 1
if is_assembly(path_file):
type_file = 2
elif is_drawing(path_file):
type_file = 3

sw_doc = sw_app.OpenDoc6(
os.path.abspath(path_file), type_file, 1, "", VT_BYREF, VT_BYREF
)

# Get the filename
_, filename = os.path.split(path_file)
Expand All @@ -61,6 +65,11 @@ def open_drawing(sw_app, path_file: str):
return sw_doc, filename


def is_drawing(path: str) -> bool:
"""Return True if paths point to a drawing file"""
return ".SLDDRW" in path.upper()


def is_assembly(path: str) -> bool:
"""Return True if paths point to an assembly file"""
return ".SLDASM" in path.upper()
Expand Down
2 changes: 2 additions & 0 deletions pyswtools/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .auto_exporter.main import auto_export
from .stat.main import stat
from .clean.main import clean
from .properties.main import properties


__version__ = "0.7.2"
Expand All @@ -33,6 +34,7 @@ def cli() -> None:
cli.add_command(auto_export)
cli.add_command(stat)
cli.add_command(clean)
cli.add_command(properties)

if __name__ == "__main__":
cli()
Empty file.
138 changes: 138 additions & 0 deletions pyswtools/properties/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
"""
Give stat from an assembly
"""

import os
import click
import copy

# pylint: disable=relative-beyond-top-level
from ..utils import check_system, check_system_verbose, do_windows_clipboard
from ..helper_sw import open_app_and_file, is_temp, is_assembly, is_file, open_file

if check_system():
# pylint: disable=import-error
import win32com.client
import pythoncom

Views = []
VT_Views = win32com.client.VARIANT(pythoncom.VT_VARIANT, Views)
VT_BYREF = win32com.client.VARIANT(pythoncom.VT_BYREF | pythoncom.VT_I4, -1)


def fetch_properties(sw_doc):
"""
Get all the properties from a solidworks document and return them
"""

props = {}
for conf in sw_doc.getConfigurationNames:
ext = sw_doc.Extension.CustomPropertyManager(conf)

names_props = ext.GetNames
if names_props is None:
continue

print(f"Using configuration '{conf}':")
for name_prop in names_props:
type_prop = ext.GetType2(name_prop)

value_prop = win32com.client.VARIANT(
pythoncom.VT_BYREF | pythoncom.VT_BSTR, ""
)
resolved_value_prop = win32com.client.VARIANT(
pythoncom.VT_BYREF | pythoncom.VT_BSTR, ""
)

was_resolved = win32com.client.VARIANT(
pythoncom.VT_BYREF | pythoncom.VT_BOOL, bool()
)
link_to_property = win32com.client.VARIANT(
pythoncom.VT_BYREF | pythoncom.VT_BOOL, bool()
)

ext.Get6(
name_prop,
False,
value_prop,
resolved_value_prop,
was_resolved,
link_to_property,
)
print(f"- {name_prop}: {value_prop.value}")
props[name_prop] = {"type": type_prop, "value": value_prop.value}
return props


def apply_props_to_doc(sw_doc, props):
"""
Apply the props to sw_doc
"""

for conf in sw_doc.getConfigurationNames:
ext = sw_doc.Extension.CustomPropertyManager(conf)

for k, v in props.items():
ext.Add3(k, v["type"], v["value"], 0)


def apply_props_to_children(sw_comp_children, props):
"""
Apply the props to all the sw_comp_children
"""

for sw_child in sw_comp_children:

sw_doc = sw_child.GetModelDoc2

if sw_doc is not None:
apply_props_to_doc(sw_doc, props)


@click.command()
@click.help_option("-h", "--help")
@click.argument(
"template_path",
type=click.Path(
exists=True,
dir_okay=False,
),
)
@click.argument(
"apply_path",
type=click.Path(
exists=True,
dir_okay=False,
),
)
def properties(
template_path: str,
apply_path: str,
) -> None:
"""
Act on properties
"""
if not check_system_verbose():
return

if not is_file(template_path) or is_temp(template_path):
click.echo("Please select a part file (.SLDPRT)")
click.echo(f"{template_path} is not an assembly file")
return

if is_temp(apply_path):
click.echo("Please select a part (.SLDPRT) or an asssembly file (.SLDASM)")
click.echo(f"{template_path} is not an part or an assembly file")
return

sw_app, sw_doc, _ = open_app_and_file(template_path)

props = fetch_properties(sw_doc)

if is_file(apply_path):
sw_doc, _ = open_file(sw_app, apply_path)
apply_props_to_doc(sw_doc, props)
else:
sw_doc, _ = open_file(sw_app, apply_path)
sw_comps = sw_doc.GetComponents(False)
apply_props_to_children(sw_comps, props)
2 changes: 1 addition & 1 deletion pyswtools/stat/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ def complete_info_on_list(sw_comp_children, dict_of_comp: dict) -> dict:
# Get the name
sw_child_name = get_clean_name(sw_child)

# Get the info fo the assembly
# Get the info of the assembly
child = complete_info_assembly(sw_child, dict_of_comp)

# If the child is already here, only increase the number
Expand Down

0 comments on commit 8f834d9

Please sign in to comment.