[macro]Mirror a hole featureset?

[macro]Mirror a hole featureset?

danmic7JH66
Advocate Advocate
1,275 Views
17 Replies
Message 1 of 18

[macro]Mirror a hole featureset?

danmic7JH66
Advocate
Advocate

Trying to mirror all my project entities and I can mirror pretty much everything but featuresets. Anyone have a way around this?

1,276 Views
17 Replies
Replies (17)
Message 2 of 18

artur.boszczyk
Advocate
Advocate

maybe someone has a macro which can mirror all holes in the feature set? Please share

0 Likes
Message 3 of 18

krasimir.vasilev
Participant
Participant

hi,
this doesn't look straight forward...
have you considered to export the hole featureset as *.XLM and manipulate the file in python and re-import the new xml

import xml.etree.ElementTree as ET

# Parse the XML file
tree = ET.parse('features.xml')
root = tree.getroot()

# Define the namespace
namespaces = {'ns': 'x-schema:Features.xml'}

# Iterate over each 'compound_hole' element in the XML
for compound_hole in root.findall('.//ns:compound_hole', namespaces):
    # Create a new 'compound_hole' element
    new_compound_hole = ET.Element('compound_hole')
    
    # Copy attributes from the original 'compound_hole' to the new one
    new_compound_hole.attrib = compound_hole.attrib.copy()
    
    # Append '_mirrorX' to the 'name' attribute of the new 'compound_hole'
    new_compound_hole.set('name', new_compound_hole.get('name') + '_mirrorX')
    
    # Iterate over each 'point' element in the original 'compound_hole'
    for point in compound_hole.findall('ns:point', namespaces):
        # Create a new 'point' element
        new_point = ET.Element('point')
        
        # Copy attributes from the original 'point' to the new one
        new_point.attrib = point.attrib.copy()
        
        # Change the sign of the 'X' attribute of the new 'point'
        new_point.set('X', str(-float(new_point.get('X'))))
        
        # Add the new 'point' to the new 'compound_hole'
        new_compound_hole.append(new_point)
    
    # Iterate over each 'axis' element in the original 'compound_hole'
    for axis in compound_hole.findall('ns:axis', namespaces):
        # Create a new 'axis' element
        new_axis = ET.Element('axis')
        
        # Copy attributes from the original 'axis' to the new one
        new_axis.attrib = axis.attrib.copy()
        
        # Add the new 'axis' to the new 'compound_hole'
        new_compound_hole.append(new_axis)
    
    # Add the new 'compound_hole' to the parent of the original 'compound_hole'
    compound_hole.getparent().append(new_compound_hole)

# Write the modified XML to a new file
tree.write('features_modified.xml')

# Print a success message
print("The XML file was successfully modified and saved as 'features_modified.xml'.")


 

0 Likes
Message 4 of 18

icse
Collaborator
Collaborator

try this:

if not entity_exists(entity('featureset','')) {
	message info 'no active featureset'
	return
}

ENTITY $featureset = entity('featureset','')

string List $mirrorPlanes = {'Y-Z','X,Z'}

int $plane = input Choice $mirrorPlanes 'Select mirror plane'

ACTIVATE WORKPLANE FROMENTITY FEATURESET $featureset

EDIT FEATURESET ; DESELECT ALL

Graphics lock
if $plane == 0 {
	foreach $feature in components($featureset) {	
		real $newValue = $feature.WPPoint[$plane] * -1
		EDIT FEATURESET $featureset SELECT $feature
		EDIT FEATURESET ; FEATURE SELECT POSITION X $newValue
		EDIT FEATURESET ; DESELECT ALL
	}
} else {
	foreach $feature in components($featureset) {	
		real $newValue = $feature.WPPoint[$plane] * -1
		EDIT FEATURESET $featureset SELECT $feature
		EDIT FEATURESET ; FEATURE SELECT POSITION Y $newValue
		EDIT FEATURESET ; DESELECT ALL
	}	
}
graphics unlock
0 Likes
Message 5 of 18

artur.boszczyk
Advocate
Advocate

thanks for this. It is a good starting point. I'm looking for a more general - mirror about any plane, which may involve setting the axis, and not only positions.

0 Likes
Message 6 of 18

hrh46
Advocate
Advocate

Very good. 

Please add commands to ask for keeping original or not.

Thanks in advance 

0 Likes
Message 7 of 18

icse
Collaborator
Collaborator

try this:

 

if not entity_exists(entity('featureset','')) {
	message info 'no active featureset'
	return
}

bool $keepOriginal = 0
$keepOriginal = query 'Keep The Original Featureset?'




ENTITY $featureset = entity('featureset','')

if $keepOriginal {
	
	string $newFSName = new_entity_name('featureset',$featureset.Name)
	copy featureset ${featureset.Name}
	
	activate featureset ${newFSName}
	
	$featureset = entity('featureset','')
}


string List $mirrorPlanes = {'Y-Z','X,Z'}

int $plane = input Choice $mirrorPlanes 'Select mirror plane'

ACTIVATE WORKPLANE FROMENTITY FEATURESET $featureset

EDIT FEATURESET ; DESELECT ALL

Graphics lock


if $plane == 0 {
	foreach $feature in components($featureset) {	
		real $newValue = $feature.WPPoint[$plane] * -1
		EDIT FEATURESET $featureset SELECT $feature
		EDIT FEATURESET ; FEATURE SELECT POSITION X $newValue
		EDIT FEATURESET ; DESELECT ALL
	}
} else {
	foreach $feature in components($featureset) {	
		real $newValue = $feature.WPPoint[$plane] * -1
		EDIT FEATURESET $featureset SELECT $feature
		EDIT FEATURESET ; FEATURE SELECT POSITION Y $newValue
		EDIT FEATURESET ; DESELECT ALL
	}	
}
graphics unlock

 

 

also here i'm not 100%sure if it works ive still dont have a powermill licence...

 

but it should give you at least a good starting point.

Message 8 of 18

hrh46
Advocate
Advocate

Thanks again. Awesome. 

There was a little typewriting mistakes, I corrected and works

fine.

0 Likes
Message 9 of 18

evo80
Collaborator
Collaborator

@icse ,

I've been trying to rename the mirrored featureset to the original featureset name + '-Mirrored'

but I can't seem to get rid of the _1 that's added when you copy an entity?? 

0 Likes
Message 10 of 18

icse
Collaborator
Collaborator

to do this the KeepOriginal ifstatement should look something like this:

 

if not entity_exists(entity('featureset','')) {
	message info 'no active featureset'
	return
}

bool $keepOriginal = 0
$keepOriginal = query 'Keep The Original Featureset?'




ENTITY $featureset = entity('featureset','')

if $keepOriginal {
	
	string $newFSName = new_entity_name('featureset',$featureset.Name)
	copy featureset ${featureset.Name}
	
	activate featureset ${newFSName}
	
	$newFSName = $featureset.Name + '_Mirrored'
	if entity_exists('featureset',$newFSName) {
		$newFSName = new_entity_name('featureset',$newFSName)
	}
	
	$featureset = entity('featureset','')
	
	rename featureset ${featureset.Name} ${newFSName}
	
	
}


string List $mirrorPlanes = {'Y-Z','X,Z'}

int $plane = input Choice $mirrorPlanes 'Select mirror plane'

ACTIVATE WORKPLANE FROMENTITY FEATURESET $featureset

EDIT FEATURESET ; DESELECT ALL

Graphics lock


if $plane == 0 {
	foreach $feature in components($featureset) {	
		real $newValue = $feature.WPPoint[$plane] * -1
		EDIT FEATURESET $featureset SELECT $feature
		EDIT FEATURESET ; FEATURE SELECT POSITION X $newValue
		EDIT FEATURESET ; DESELECT ALL
	}
} else {
	foreach $feature in components($featureset) {	
		real $newValue = $feature.WPPoint[$plane] * -1
		EDIT FEATURESET $featureset SELECT $feature
		EDIT FEATURESET ; FEATURE SELECT POSITION Y $newValue
		EDIT FEATURESET ; DESELECT ALL
	}	
}
graphics unlock

 

 

Message 11 of 18

iamcdn79
Mentor
Mentor

 

My solution

 

if not entity_exists(entity('featureset','')) {
	message info 'no active featureset'
	return
}

bool $keepOriginal = 0
$keepOriginal = query 'Keep The Original Featureset?'

ENTITY $featureset = entity('featureset','')

string $newFSName = new_entity_name('featureset',$featureset.Name)

if $keepOriginal {
	copy featureset ${featureset.Name}
	// Activate the copied featureset
	activate featureset ${newFSName}
	$featureset = entity('featureset','')
}

string List $mirrorPlanes = {'YZ','XZ'}

int $plane = input Choice $mirrorPlanes 'Select mirror plane'

ACTIVATE WORKPLANE FROMENTITY FEATURESET $featureset

EDIT FEATURESET ; DESELECT ALL

Graphics lock

if $plane == 0 {
	foreach $feature in components($featureset) {	
		real $newValue = $feature.WPPoint[$plane] * -1
		EDIT FEATURESET $featureset SELECT $feature
		EDIT FEATURESET ; FEATURE SELECT POSITION X $newValue
		EDIT FEATURESET ; DESELECT ALL
	}
} else {
	foreach $feature in components($featureset) {	
		real $newValue = $feature.WPPoint[$plane] * -1
		EDIT FEATURESET $featureset SELECT $feature
		EDIT FEATURESET ; FEATURE SELECT POSITION Y $newValue
		EDIT FEATURESET ; DESELECT ALL
	}
}

graphics unlock

// Add "-Mirrored" to the end of the featureset name
$newFSName = $newFSName + "-Mirrored"

// Rename the featureset with the new name
rename featureset ${featureset.Name} ${newFSName}

 


Intel Core i9 13900KF CPU
128 GB Kingston Beast DDR4 SDRAM
PNY RTX A2000 6GB Video Card
WD 1 TB SSD Hard Drive
Windows 11 Pro

Message 12 of 18

evo80
Collaborator
Collaborator

@icse ,

Cheers, that nailed it!

0 Likes
Message 13 of 18

artur.boszczyk
Advocate
Advocate
0 Likes
Message 14 of 18

evo80
Collaborator
Collaborator

@iamcdn79 

That is what I did originally, only thing with this is it doesn't rid the _1 or whatever when you copy an entity,

however, the code from @icse does.

Thanks for the help btw!

0 Likes
Message 15 of 18

iamcdn79
Mentor
Mentor

@icse if the workplane from the featureset doesn't exist would this be a good way to use the world workplane instead? 

if not entity_exists(entity('featureset','')) {
    message info 'No active featureset'
    return
}

bool $keepOriginal = 0
$keepOriginal = query 'Do you want to copy the active hole featureset?'

ENTITY $featureset = entity('featureset','')

string $newFSName = new_entity_name('featureset', $featureset.Name)

if $keepOriginal {
    copy featureset ${featureset.Name}
    // Activate the copied featureset
    activate featureset ${newFSName}
    $featureset = entity('featureset','')
}

string List $mirrorPlanes = {'YZ','XZ'}

int $plane = input Choice $mirrorPlanes 'Select mirror plane'

// Check if the workplane entity exists
bool $workplaneExists = entity_exists(entity('workplane', $featureset.Name))

if $workplaneExists {
    ACTIVATE WORKPLANE FROMENTITY FEATURESET $featureset
} else {
    // Activate a default workplane if the specified workplane does not exist
    ACTIVATE WORKPLANE " "
}

EDIT FEATURESET ; DESELECT ALL

Graphics lock

if $plane == 0 {
    foreach $feature in components($featureset) {    
        real $newValue = $feature.WPPoint[0] * -1  // X value
        if not $workplaneExists {
            // Apply the transformation using the world workplane
            $newValue = $feature.Point[0] * -1  // X value using world workplane
        }
        EDIT FEATURESET $featureset SELECT $feature
        EDIT FEATURESET ; FEATURE SELECT POSITION X $newValue
        EDIT FEATURESET ; DESELECT ALL
    }
} else {
    foreach $feature in components($featureset) {    
        real $newValue = $feature.WPPoint[1] * -1  // Y value
        if not $workplaneExists {
            // Apply the transformation using the world workplane
            $newValue = $feature.Point[1] * -1  // Y value using world workplane
        }
        EDIT FEATURESET $featureset SELECT $feature
        EDIT FEATURESET ; FEATURE SELECT POSITION Y $newValue
        EDIT FEATURESET ; DESELECT ALL
    }
}

graphics unlock

// Add "mirror" to the end of the featureset name
$newFSName = $newFSName + " mirrored "

// Add the workplane used to mirror to the end of the featureset name
if $plane == 0 {
    $newFSName = $newFSName + " in the X"
} else {
    $newFSName = $newFSName + " in the Y"
}

// Rename the featureset with the new name
rename featureset ${featureset.Name} ${newFSName}

 


Intel Core i9 13900KF CPU
128 GB Kingston Beast DDR4 SDRAM
PNY RTX A2000 6GB Video Card
WD 1 TB SSD Hard Drive
Windows 11 Pro

0 Likes
Message 16 of 18

icse
Collaborator
Collaborator

With my macro if the featureset does not got a workplane the line

 

ACTIVATE WORKPLANE FROMENTITY FEATURESET $featureset

 

should deactivate the current active workplane eg activate the global workplane

then the holes should be mirrored arround the global workplane.

 

No edit necessary

0 Likes
Message 17 of 18

iamcdn79
Mentor
Mentor

it didn't work with this code, it just left the featureset hole where it was

 

if not entity_exists(entity('featureset','')) {
	message info 'no active featureset'
	return
}

bool $keepOriginal = 0
$keepOriginal = query 'Do you want to copy the active hole featureset?'

ENTITY $featureset = entity('featureset','')

string $newFSName = new_entity_name('featureset',$featureset.Name)

if $keepOriginal {
	copy featureset ${featureset.Name}
	// Activate the copied featureset
	activate featureset ${newFSName}
	$featureset = entity('featureset','')
}

string List $mirrorPlanes = {'YZ','XZ'}

int $plane = input Choice $mirrorPlanes 'Select mirror plane'

ACTIVATE WORKPLANE FROMENTITY FEATURESET $featureset

EDIT FEATURESET ; DESELECT ALL

Graphics lock

if $plane == 0 {
	foreach $feature in components($featureset) {	
		real $newValue = $feature.WPPoint[$plane] * -1
		EDIT FEATURESET $featureset SELECT $feature
		EDIT FEATURESET ; FEATURE SELECT POSITION X $newValue
		EDIT FEATURESET ; DESELECT ALL
	}
} else {
	foreach $feature in components($featureset) {	
		real $newValue = $feature.WPPoint[$plane] * -1
		EDIT FEATURESET $featureset SELECT $feature
		EDIT FEATURESET ; FEATURE SELECT POSITION Y $newValue
		EDIT FEATURESET ; DESELECT ALL
	}
}

graphics unlock

// Add "mirror" to the end of the featureset name
$newFSName = $newFSName + " mirrored "

// Add the workplane used to mirror to the end of the featureset name
if $plane == 0 {
	$newFSName = $newFSName + "in the X"
} else {
	$newFSName = $newFSName + "in the Y"
}

// Rename the featureset with the new name
rename featureset ${featureset.Name} ${newFSName}

 


Intel Core i9 13900KF CPU
128 GB Kingston Beast DDR4 SDRAM
PNY RTX A2000 6GB Video Card
WD 1 TB SSD Hard Drive
Windows 11 Pro

0 Likes
Message 18 of 18

icse
Collaborator
Collaborator

You right it does not work correctly if the featureset does use the global workplane....

But it actually should work 😛

 

if you keep alternating between the mirror planes x-z / y-z it seems to work

if you use the same plane multipe times in a row (mirror the featureset back) it stops working but i dont know why...

 

if the featureset got a linked workplane everything seems to work fine.

0 Likes