OneSpace Modeling: Deleting all parts matching a pattern

This example demonstrates how to combine the selection facilities in sd-defdialog and pattern matching. The dialog takes two inputs:

  • A list of parts (use the standard selection mechanisms to build the list, for example "select recursively all parts in assembly", "all at top", "all in 3D box" etc.)
  • A pattern which the names of the selected parts are compared against

The code scans all selected parts and compares their names with the specified pattern. If the pattern matches, the part is deleted.

sd-string-match-pattern-p accepts a number of special characters and wildcards - see the Developer's Kit documentation (reference manual, section on "String Handling") for details. Examples:

  • part* matches part, part42, party etc.
  • *part* matches depart, departure, part etc.
  • p?rt matches part, port and pirt (whatever the latter may be big grin )
  • [a-z]art matches part and dart

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Description:  Deletes parts matching a pattern
;; Author:       Claus Brod
;; Created:      2/11/2005 20:20    
;; Language:     Lisp
;;
;; (C) Copyright 2005 Claus Brod, all rights reserved
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
(in-package :clausbrod.de)
(use-package :oli)

(sd-defdialog 'DELETE_MATCHING_PARTS
  :dialog-title "Delete matching parts" :toolbox-button t

  :variables '(
    (parts :value-type :part :multiple-items t)
    (pattern :value-type :string :initial-value "*42*"))

  :local-functions '(
    (delete-item (item)
      (when (sd-string-match-pattern-p pattern (sd-inq-obj-basename item))
        (sd-call-cmds (delete_3d item)))) )

  :ok-action '(mapc #'delete-item parts)
)

Thanks to Stephan Wörz for the inspiration.

If you also need to delete assemblies which match a pattern, you could simply change the variable type for parts from :part to :part-assembly. However, it is not clear what happens in the following scenario:

  • Selection returns /a1pattern/ and /a1pattern/p1pattern
  • delete-item is called for /a1pattern and deletes the assembly (including /a1pattern/p1pattern
  • delete-item is called again, this time for /a1pattern/p1pattern and tries to delete it although it no longer exists.

-- ClausBrod - 11 Feb 2005

See MacroSwapColor to see how the approach above can be used to find all parts with a specified color, and change it to some new color.

-- ClausBrod - 20 Mar 2006


When asked for a TWiki account, use your own or the default TWikiGuest account.

Revision: r1.8 - 18 Dec 2006 - 20:37 - ClausBrod
CoCreateModeling > OsdmMacros > MacroDeleteMatchingParts
Copyright © 1999-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback