Developing a Vectorworks 2011 Plug-in, TDD-style – Episode 4

In Episode 3 – Not doing the same sketch twice, we made some good progress on a class finally capable of describing the geometry of the cabinet’s parts. In this episode, we will create Vectorworks geometry. It seems like a good idea to separate the creation of the geometry from the actual model (much in the way a View is separated from a Model in MVC). Let’s call the new SimpleCabinetCreator:

class SimpleCabinetCreator {
	
public:
	
	static void CreateFrom(const SimpleCabinetModel& model) {
		
		MCObjectHandle leftSide = gSDK->CreateExtrude(0, model.GetHeight());
		gSDK->AddObjectToContainer(gSDK->CreateRectangle(model.GetLeftSideBase()), leftSide);
		
		MCObjectHandle rightSide = gSDK->CreateExtrude(0, model.GetHeight());
		gSDK->AddObjectToContainer(gSDK->CreateRectangle(model.GetRightSideBase()), rightSide);
		
		MCObjectHandle top = gSDK->CreateExtrude(model.GetTopZPosition(), model.GetTopZPosition() + model.kBoardThickness);
		gSDK->AddObjectToContainer(gSDK->CreateRectangle(model.GetTopBottomBase()), top);
		
		MCObjectHandle bottom = gSDK->CreateExtrude(0, model.kBoardThickness);
		gSDK->AddObjectToContainer(gSDK->CreateRectangle(model.GetTopBottomBase()), bottom);
		
	}
	
};

Looks good so far. The main takeaway point is that the Creator class shouldn’t do any calculation. It should be translating the model straight into Vectorworks geometry without any logic on it’s own.

While we’re at it: How about renaming SimpleCabinet to SimpleCabinetModel? This would be a nice symmetrical way of naming the classes: SimpleCabinetModel and SimpleCabinetModel. Let’s do that.

Now for the grand finale of this episode. The plug-in object’s Recalculate() method is pretty straightforward. All the basic building blocks are already in place:

EObjectEvent CObject_EventSink::Recalculate()
{
	VWParametricObj object(fhObject);
	double width = object.GetParamReal("Width");
	double height = object.GetParamReal("Height");
	double depth = object.GetParamReal("Depth");
	
	SimpleCabinetModel simpleCabinetModel(width, height, depth);
	SimpleCabinetCreator::CreateFrom(simpleCabinetModel);
	
	return kObjectEventNoErr;
}

We get the width, height & depth from the parametric object, create a model and ask the SimpleCabinetCreator to create the geometry defined in the model. Let’s fire up Vectorworks and see if everything is working as expected. And – much to our surprise – it’s working out as expected:

Episode4

Sounds like a great way to end this episode. Let’s see if we can add the front tiles in the upcoming episodes. TDD-style, of course.

Make sure to tune in next week when you hear Dr. Bob say: “A third class? I’m a quack, not a CS major.”.

Previous Episode | Next Episode