Jump to content

Attach record to custom plug-in object


Recommended Posts

Hello! 

 

Is it possible to attach a record to a custom plug-in object automatically as part of the script of the custom plug-in? And then assign values to the record fields?

 

I tried a combination of vs.GetCustomObjectInfo()[1] to get the handle to the custom plug-in object and then vs.SetRecord() to set the record to an existing record format, but it doesn't actually attach the record. A test file along with the .vso of the custom plug-in are attached. It creates a rectangle and then extrudes it based on dimensions provided by the custom plug-in parameters. And it should assign those dimensions to the record. But it doesn't even attach the record. The test is very simple to get up and running, but I do have a practical use case that is similar but with a little more to it. 

 

Here is the script for the custom plug-in.

 

# inputs
width = vs.Pwidth
depth = vs.Pdepth
height = vs.Pheight

# define variables
PIO_handle = vs.GetCustomObjectInfo()[1]
direction = [1, 0]  # for creating extrudes

# Create the extrude
bottom = 0
top = height
origin = [-width / 2, -depth / 2]
vs.BeginXtrd(bottom, top)
prof = vs.RectangleN(origin[0], origin[1], direction[0], direction[1], width, depth)
vs.EndXtrd()

# Attach record and set Pedestal RF fields
blockRecord = "Box RF"
vs.SetRecord(PIO_handle, blockRecord)
vs.SetRField(PIO_handle, blockRecord, "Width", width)
vs.SetRField(PIO_handle, blockRecord, "Depth", depth)
vs.SetRField(PIO_handle, blockRecord, "Height", height)

 

Box Custom Plug-in.vsoCustom Plug-in Object Record Test.vwx

Edited by Anthony Esau
Update test file
Link to comment

The simple answer is that GetCustomObjectInfo() returns the handle to the running PIO as the THIRD parameter, so you want:

 

PIO_handle = vs.GetCustomObjectInfo()[2]

 

However, I recommend an number of additional considerations:

PIOs actually save their parameter data into a record, which is accessible from worksheets and scripts, so depending on what you are trying to accomplish, this may be an unnecessary step

 

Before attaching the record format, you should check to see that it exists in the drawing, and if not create it.

 

Attached records persist through object regeneration, so instead of attaching each time, you should first check if the record is already attached. I recommend using Eval for this.

 

SetRField expects a string as the value. I'm not sure if the rules for python are less strict than Vectorscript, but if so, and the record uses a dimension field, you'll want to use Num2StrF()

  • Like 1
Link to comment

Ah, I didn't read the documentation for GetCustomObjectInfo() closely enough and missed one of the returned values which threw off my numbering. Thank you for identifying the mistake, @JBenghiat!

 

Good point about accessing the object parameters rather than attaching a potentially redundant record. I have already been using the object parameters as dynamic text in a graphic legend, but I would like to do some calculations with the parameter values and make that information available to the legend. And I may attach different records based on the settings of the object. My documentation pipeline for this object is still being worked out, so we'll see if it plays out effectively. By the way, the PIO is a museum pedestal that can be made in a few different styles which affect the relative size of the vitrine (acrylic case) on top of it. 

 

Here's a snippet of the actual work-in-progress PIO script (not the test from above) incorporating the other suggestions. Thanks so much for your input @JBenghiat. So far this is working in my limited testing. 

pedestal_record = "Pedestal RF"

# Check if record format exists
pedestal_record_handle = vs.GetObject(pedestal_record)
if pedestal_record_handle == 0:
	# Create record format
	vs.NewField(pedestal_record , "Style", "Step", 4, 0)
	vs.NewField(pedestal_record , "Width", 0, 9, 0)
	vs.NewField(pedestal_record , "Depth", 0, 9, 0)
	vs.NewField(pedestal_record , "Height", 0, 9, 0)
	vs.NewField(pedestal_record , "Include Vitrine", True, 2, 1)
	
# Check if record is attached to PIO
if vs.Eval(PIO_handle, "R IN ['"+ pedestal_record + "']") == 0:
	# Attach record
	vs.SetRecord(PIO_handle, pedestal_record)

# Set field values
vs.SetRField(PIO_handle, pedestal_record, "Style", style)
vs.SetRField(PIO_handle, pedestal_record, "Width", vs.Num2StrF(width))
vs.SetRField(PIO_handle, pedestal_record, "Depth", vs.Num2StrF(depth))
vs.SetRField(PIO_handle, pedestal_record, "Height", vs.Num2StrF(height))
vs.SetRField(PIO_handle, pedestal_record, "Include Vitrine", includeVitrine)

 

Edited by Anthony Esau
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...