Code Monkey home page Code Monkey logo

fcxref's Introduction

fcxref

PyPI version


Introduction

Manages FreeCAD external references.

"External references" are also known as external links or cross-document references.

The following operations are supported:

  1. Finding external references
  2. Renaming external references
  3. and Removing external references (i.e. XLinks)

Motivation

On Expressions: Known issues / remaining tasks, it's mentioned:

There is no expression manager implemented where all expressions in a document are listed, and can be created, deleted, queried, etc.

Large complex FreeCAD projects typically rely on extensive use of cross-document referencing to properties such as aliases in spreadsheets.

When you have dozens of references to the same property, it becomes very difficult to find all the places where references exist or rename the property.

fcxref aims to fill this gap until similiar functionality can be added to FreeCAD core.

See the following related FreeCAD forum discussions for additional motivation:

Approach

fcxref relies on parsing the Document.xml in compressed .FCStd files.

Installation

Available on the Python Package Index (PyPI).

pip install fcxref

Usage

There are two ways to use fcxref:

  1. via the Python API
  2. vai the Command Line

The following 2 sections cover these 2 usage methods with documents in the example/ directory.

Consider you have a MainDocument.FCStd containing a spreadsheet that drives your model, and ExampleDocument.FCStd that references aliases in that spreadsheet.

Python API

find

from fcxref import find, Query

base_path = './example'
references = find(base_path, Query('MainDocument', 'Spreadsheet', 'Value'))
print('\n'.join(map(str, references)))
ExampleDocument Spreadsheet.B1 content
ExampleDocument Spreadsheet.A1 content
ExampleDocument Spreadsheet.B1 alias
ExampleDocument Box.Length expression

rename

The rename function takes:

  1. the base path to look for FreeCAD documents in
  2. the name or label of the document
  3. the name or label of the object
  4. and a 2-element tuple containing the property before and after renaming.

It returns a dictionary where keys are filepaths to updated .FCStd files, and values are XML Element objects representing updated Document.xml files.

from fcxref import rename

base_path = './example'
root_by_document_path = rename(base_path, 'MainDocument', 'Spreadsheet', ('Value', 'RenamedValue'))
print(root_by_document_path)
{'ExampleDocument.FCStd': <Element 'Document' at 0x7efcd281cc20>, 'MainDocument.FCStd': <Element 'Document' at 0x7f4d13c39270>}

remove

The remove function takes:

  1. the base path to look for FreeCAD documents in
  2. the name of the document (label is not supported)

It returns a dictionary where keys are filepaths to updated .FCStd files, and values are XML Element objects representing updated Document.xml files.

from fcxref import remove

base_path = './example'
root_by_document_path = remove(base_path, 'MainDocument')
print(root_by_document_path)
{'ExampleDocument.FCStd': <Element 'Document' at 0x7efcd281cc20>}

Command Line

Upon installing fcxref, the fcxref command will become globally accessible.

For usage information, pass --help to each command.

Each command scans for *.FCStd files recursively from the current working directory.

Thus, you should navigate to a directory where you store your FreeCAD documents before executing fcxref commands.

$ fcxref --help ↵
usage: fcxref [-h] [--version] {find,rename,remove} ...

Manage cross-document references to properties.

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit

Commands:
  {find,rename,remove}
    find                Find cross-document references to an object or property
    rename              Rename cross-document references to a property
    remove              Remove XLinks to specified document

find

$ fcxref find --help ↵                
usage: fcxlink find <document> <object> [property]

Surround arguments containing special characters in quotes (e.g. "<<My Label>>").

positional arguments:
  document    Document name or label.
  object      Object name or label.
  property    Property.

optional arguments:
  -h, --help  show this help message and exit

Simple Queries

$ fcxref find MainDocument Spreadsheet Value ↵
ExampleDocument Spreadsheet.B1 content direct
ExampleDocument Spreadsheet.A1 content indirect
ExampleDocument Spreadsheet.B1 alias indirect
ExampleDocument Box.Length expression indirect

💡 TIP: When using special characters on the command line such as < and > for label names, surround the argument in double-quotes.

rename

$ fcxref rename --help ↵
usage: fcxlink rename <document> <object> <from_property> <to_property>

Surround arguments containing special characters in quotes (e.g. "<<My Label>>").

positional arguments:
  document       Document name or label of reference to rename.
  object         Object name or label of reference to rename.
  from_property  Property of reference before renaming.
  to_property    Property of reference after renaming.

optional arguments:
  -h, --help     show this help message and exit

Simple Renames

The rename command will prompt users for confirmation before modifying any files, and defaults to "No" if an explicit "Yes" is not provided.

$ fcxref rename MainDocument Spreadsheet Value RenamedValue ↵
The following 2 document(s) reference MainDocument#Spreadsheet.Value:
  ExampleDocument.FCStd
  MainDocument.FCStd

Do you wish to rename references to MainDocument#Spreadsheet.RenamedValue? [y/N] 
y ↵
2 document(s) updated.

remove

$ fcxref remove --help ↵
usage: fcxlink remove <document>

Surround arguments containing special characters in quotes (e.g. "<<My Label>>").

positional arguments:
  document    Document name of XLinks to remove.

optional arguments:
  -h, --help  show this help message and exit

Simple Removals

The remove command will prompt users for confirmation before modifying any files, and defaults to "No" if an explicit "Yes" is not provided.

$ fcxref remove MainDocument ↵
The following 1 document(s) contain XLinks to MainDocument:
  example/ExampleDocument.FCStd

Do you wish to remove XLinks to MainDocument? (this will break document linking) [y/N] 
y ↵
1 document(s) updated.

Supported FreeCAD Versions

Currently only FreeCAD 19 and greater is supported.

If changes are minimal, then supporting older versions may be considered.

Changelog

See Changelog.

Contributing

See Contributing Guidelines.

fcxref's People

Contributors

gbroques avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

fcxref's Issues

Implement Text Search

It's common for spreadsheets referencing a property in another document to duplicate that as a label in a nearby cell or alias in it's internal spreadsheet.

We could possibly implement this as a simple "text search" and have separate find and replace functions for "text references".

Pattern would be r'\bText\b'.

Changing an internal alias is somewhat complicated as you need to also change all references to that alias.

FreeCAD implements this by default, so maybe it's not worth addressing.

This search could be performed after a rename operation, on only the documents that were renamed.

Directories missing from PyPI installation

Some directories are missing from the PyPI installation (at least fcxref/{find,remove,rename}).

pip3 show fcxref --files gives:

Name: fcxref
Version: 0.3.0
Summary: Manages FreeCAD external references.
Home-page: https://github.com/gbroques/fcxref
Author: G Roques
Author-email: None
License: UNKNOWN
Location: /home/gael/.local/lib/python3.8/site-packages
Requires: 
Required-by: 
Files:
  ../../../bin/fcxref
  fcxref-0.3.0.dist-info/INSTALLER
  fcxref-0.3.0.dist-info/METADATA
  fcxref-0.3.0.dist-info/RECORD
  fcxref-0.3.0.dist-info/WHEEL
  fcxref-0.3.0.dist-info/entry_points.txt
  fcxref-0.3.0.dist-info/top_level.txt
  fcxref/__init__.py
  fcxref/__pycache__/__init__.cpython-38.pyc
  fcxref/__pycache__/_version.cpython-38.pyc
  fcxref/__pycache__/cli.cpython-38.pyc
  fcxref/__pycache__/group_references_by_document_path.cpython-38.pyc
  fcxref/__pycache__/root_by_document_path.cpython-38.pyc
  fcxref/_version.py
  fcxref/cli.py
  fcxref/group_references_by_document_path.py
  fcxref/root_by_document_path.py

Anyway, thanks for offering this tool to the community!
Gaël

Rename XLinks Upon Object or Document Rename

There are references to document and object names in XLinks that should be renamed when renaming a reference.

Need to be careful to not to rename an XLink if another reference to that file or object still exists.

In this case we would add an XLink and increment the count and docs attribute.

<XLinks count="1" docs="1">
    <DocMap name="Master" label="Master" index="0"/>
    <XLink file="Master.FCStd" stamp="2021-07-25T18:40:15Z" name="Spreadsheet"/>
</XLinks>
<XLinks count="2" docs="2">
    <DocMap name="Master" label="Master" index="1"/>
    <DocMap name="Cube" label="Cube" index="0"/>
    <XLink file="Cube.FCStd" stamp="2021-07-25T20:03:03Z" name="Box"/>
    <XLink file="Master.FCStd" stamp="2021-07-25T18:40:15Z" name="Spreadsheet"/>
</XLinks>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.