Message 1 of 5
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello everybody,
I'm having trouble connecting a JSON file to Python. I'm currently making a custom valve with numerous parameters. I created a JSON file and added parameters to it to reduce the number of parameters. But it's not working. You can see code from JSON and python.
Someone can help me fix that.
Thank you!
{
"1": { "D": 8.625, "L": 15.0, "H": 8.625 },
"2": { "D": 10.625, "L": 25.0, "H": 10.625 },
"3": { "D": 12.0, "L": 36.0, "H": 12.0 }
}from aqa.math import *
from varmain.primitiv import *
from varmain.var_basic import *
from varmain.custom import *
import json
import os
import sys
sys.path.append(os.path.normpath(sys.PnP3dNativeContentCustomScriptsPath))
class Test_Valve_Modeler:
def __init__(self, SpecParams, **kw):
# Parameters from the spec
self.s = SpecParams['s']
self.tag = SpecParams.get('TAG', 1)
self.specKw = SpecParams['specKw']
def build_model(self):
try:
self.set_values()
self.build_valve()
except Exception as e:
print(f"Error: {e}")
raise
def set_values(self):
with open('json1.json', mode='r') as file:
data = json.load(file)
part_params = data.get(str(self.tag), {})
D = part_params.get('D', 0.0)
L = part_params.get('L', 0.0)
H = part_params.get('H', 0.0)
self.OD = D
self.L = L
self.H = H
print(f"D: {D}, L: {L}, H: {H}") # Print values for debugging
def build_valve(self):
try:
oC1 = CONE(self.s, R1=self.H / 2, R2=0, H=self.L / 2)
oC1.rotateY(90)
oC2 = CONE(self.s, R1=self.H / 2, R2=0, H=self.L / 2)
oC2.rotateY(-90).translate((self.L, 0, 0))
oC3 = self.build_wheel()
oC3.translate((self.L * 0.5, 0.0, 0.0))
oC1.uniteWith(oC2)
oC2.erase()
oC1.uniteWith(oC3)
oC3.erase()
self.s.setPoint((0, 0, 0), (-1, 0, 0))
self.s.setPoint((self.L, 0, 0), (1, 0, 0))
except Exception as e:
print(f"Valve Error: {e}")
raise
def build_wheel(self):
try:
R1 = self.OD / 30
R2 = self.OD / 40
R3 = self.OD / 2 - R1
self.L = self.L - R1
oT0 = TORUS(self.s, R1=R3, R2=R1).translate((0, 0, self.L))
oC1 = CYLINDER(self.s, R=R2, H=R3 * 2, O=0).rotateX(90).translate((0, R3, self.L)).rotateZ(45)
oC2 = CYLINDER(self.s, R=R2, H=R3 * 2, O=0).rotateY(90).translate((-R3, 0, self.L)).rotateZ(45)
oC3 = CYLINDER(self.s, R=R2, H=self.L, O=0)
oC1.uniteWith(oC2)
oC2.erase()
oC1.uniteWith(oC3)
oC3.erase()
oC1.uniteWith(oT0)
oT0.erase()
return oC1
except Exception as e:
print(f"Wheel Error: {e}")
raise
@activate(Group='Valve, ValveBody', TooltipShort='TestValve', TooltipLong='TestValve', LengthUnit='in', Ports='2')
@group('MainDimensions')
@param(TAG=ENUM, TooltipLong='1, 2, 3')
@enum(1, 'ValveTag1')
@enum(2, 'ValveTag2')
@enum(3, 'ValveTag3')
def TestValve(s, TAG=1, **kw):
try:
spec_params = {'s': s, 'TAG': TAG, 'specKw': kw}
valve = Test_Valve_Modeler(spec_params, **kw)
valve.build_model()
except Exception:
print("Failed to build a valve")
so
Solved! Go to Solution.