HOW TO LINK DATA FROM JSON FILE TO PYTHON

HOW TO LINK DATA FROM JSON FILE TO PYTHON

traiduong014969
Collaborator Collaborator
623 Views
4 Replies
Message 1 of 5

HOW TO LINK DATA FROM JSON FILE TO PYTHON

traiduong014969
Collaborator
Collaborator

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!

traiduong014969_0-1693206087547.png

 

 

{
    "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 

0 Likes
Accepted solutions (2)
624 Views
4 Replies
Replies (4)
Message 2 of 5

matt.worland
Advisor
Advisor
Accepted solution

You were pretty close with reading the data. Using the below method works for me.

def set_values(self):
    try:
        with open(r"C:\AutoCAD Plant 3D 2024 Content\CPak Common\CustomScripts\test_valve.json", mode='r') as file:
            data = json.load(file)
        part_params = data[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
    except Exception as e:
        print(f"Set Valve Error: {e}")
        raise
If my reply was helpful, please give a "Thumbs Up" or "Accept as Solution"
Message 3 of 5

traiduong014969
Collaborator
Collaborator
Thank for your reply.
Yes, I just tested it, and it works. So I have an issue:
Your solution works when the path is right with the AutoCad version. However, with the AutoCad version lower, This file will not work. So there's another way it's compatible with most versions?
0 Likes
Message 4 of 5

matt.worland
Advisor
Advisor
Accepted solution
You should be able to put that file anywhere you would like, as long as Python in P3D has access to its location in here sys.path
If my reply was helpful, please give a "Thumbs Up" or "Accept as Solution"
0 Likes
Message 5 of 5

traiduong014969
Collaborator
Collaborator
Thank you matt.worland for help. It's working for me
0 Likes