Jump to content

Auto Creation of Comments (Python Script)


---
 Share

Recommended Posts

Hello,

I am creating a script that does a large variety of different things and I'm having an issue with adding comments to features. 

I have attached the code I am having an issue with below. For the holes (for hole in holes:) it works fine. The script creates a feature, applies an X tolerance, creates a Gray value feature and inserts a comment to the nominal element based on the CSV file imported. It then continues to iterate through the imports and  applies all the correct comments to each feature. 

However when I try and do the same with the mating features list (surface points). It applies the comment to the actual element and not the nominal element. Even though the code is the same and works fine on circles.  

I've tried moving the edit_comment code to below the create_surface_point function and it will apply the comment to the nominal element however the software then gives me an error that no object was selected to apply the X dimension or inspection principle to.  

Does anyone have any ideas of how to fix this issue? 

As stated it works fine on holes/circles just surface and edge points that are causing an issue. 


for hole in holes:
	feature_name = hole[2]
	point_x = float(hole[4])
	point_y = float(hole[5])
	point_z = float(hole[6])
	direction_x = float(hole[7])
	direction_y = float(hole[8])
	direction_z = float(hole[9])
	radius = float(hole[18])
	lower_tolerance_x = float(hole[19]) if hole[19] else None
	upper_tolerance_x = float(hole[20]) if hole[20] else None
	level_comm = hole[3] if hole[3] else None

	if feature_name in existing_features:
		print(f"Feature '{feature_name}' already exists, skipping...")
		hole_skipped_count += 1
		continue

	MCAD_ELEMENT = gom.script.primitive.create_auto_nominal_circle(
		name=feature_name,
		normal={
			'direction': gom.Vec3d(direction_x, direction_y, direction_z),
			'point': gom.Vec3d(point_x, point_y, point_z),
			'type': 'projected'
		},
		point={
			'point': gom.Vec3d(point_x, point_y, point_z)
		},
		radius=radius,
	)

	if lower_tolerance_x is not None and lower_tolerance_x != '' and upper_tolerance_x is not None and upper_tolerance_x != '':
		gom.script.inspection.inspect_dimension(
			elements=[MCAD_ELEMENT],
			nominal_value_source='from_nominal',
			tolerance={'lower': float(lower_tolerance_x), 'upper': float(upper_tolerance_x)},
			type='x'
		)

	measurement_result = gom.script.inspection.measure_by_gray_value_feature(
		diagonally_punched_hole=False,
		elements=[MCAD_ELEMENT],
		material_thickness=1.5,
		parameter_template={
			'contrast': 20,
			'element_fitting_quality': 'strict',
			'manufacturing_method': 'punched_against_normal_direction',
			'material_color': 'bright',
			'max_observation_angle': 0.7853981634,
			'min_observation_angle': 0.0,
			'name': 'Default',
			'plane_fitting_quality': 'strict',
			'surface_type': 'sheet_metal_shiny'
		}
	)

	MCAD_ELEMENT = gom.script.sys.edit_properties (
		data=MCAD_ELEMENT, 
		elem_comment=level_comm,
	)


for mating in mating:
	feature_name = mating[2]
	point_x = float(mating[4])
	point_y = float(mating[5])
	point_z = float(mating[6])
	direction_x = float(mating[7])
	direction_y = float(mating[8])
	direction_z = float(mating[9])
	lower_tolerance_x = float(mating[19]) if mating[19] else None
	upper_tolerance_x = float(mating[20]) if mating[20] else None
	level_comm = mating[3] if mating[3] else None

	if feature_name in existing_features:
		print(f"Feature '{feature_name}' already exists, skipping...")
		mating_skipped_count += 1
		continue
		
	MCAD_ELEMENT=gom.script.primitive.create_surface_point (
		name=feature_name, 
		normal={'direction': gom.Vec3d (direction_x, direction_y, direction_z), 'point': gom.Vec3d (0.0, 0.0, 0.0), 'type': 'projected'}, 
		point={'point': gom.Vec3d (point_x, point_y, point_z)}
	)

	if lower_tolerance_x is not None and lower_tolerance_x != '' and upper_tolerance_x is not None and upper_tolerance_x != '':
		gom.script.inspection.inspect_dimension(
			elements=[MCAD_ELEMENT],
			nominal_value_source='from_nominal',
			tolerance={'lower': float(lower_tolerance_x), 'upper': float(upper_tolerance_x)},
			type='x'
		)

	MCAD_ELEMENT=gom.script.inspection.measure_by_intersection_with_mesh (
		check_plausibility=True, 
		elements=[MCAD_ELEMENT], 
	)

	MCAD_ELEMENT = gom.script.sys.edit_properties (
		data=MCAD_ELEMENT, 
		elem_comment=level_comm,
	)

Many Thanks 

Kris

Edited
Link to comment
Share on other sites

I found the issue.

it was to do with the MCAD=ELEMENT before the X dimension, inspection and comment.

Removed the MCAD requirement and it seems to be working fine.  

Link to comment
Share on other sites

hey Kristian, I see you are getting more advanced with scripts 🙂 id be careful on the vector of the surface points,  right now from a very quick read it looks like all surface points will have 0,0,0 vector.   if your import file only contains point co-ordinates then you'll need to adapt to a surface point afterwards taking info from CAD.  hopefully that makes sense.

Link to comment
Share on other sites

Please sign in to view this quote.

Hey James,

Hope you are well. 

I'm trying. It's taking a lot of python learning, trial and error and troubleshooting but I'm getting there. 

Thank you for the note. This is just a snippet, I took from a much larger script, that I have been using to troubleshoot. The full script has defined values for the vectors based on a variable created during CSV import. 

I will now be checking they are all set up correctly though. 

 

Link to comment
Share on other sites

 Share

×
×
  • Create New...