In Episode 4 we finally saw the fruits of our labor – TDD-style. What’s left? We need to come up with some code for the front tiles / segments.
Let’s get right to it. Here the first test. We assume that there’s class FrontModel with a method GetOrigin(), which returns the position of the leftmost bottom front tile.
TEST_N(FrontModelPositionOfFirstSegment) { SimpleCabinetModel simpleCabinet(600, 720, 800); FrontModel frontModel; simpleCabinet.GetFrontModel(frontModel); CHECK_EQUAL(WorldPt(SimpleCabinetModel::kBoardThickness, 0), frontModel.GetOrigin()); }
This is another one of these “Get’s us going” tests. Not an awful lot of thought need to make it pass, but it puts us in the right mood for the next challenge.
class SimpleCabinetModel { public: // ... void GetFrontModel(FrontModel& model) const { model.SetOrigin(WorldPt(kBoardThickness, 0)); } }; class FrontModel { public: FrontModel() : fOrigin(WorldPt (0, 0)) { } WorldPt GetOrigin() { return fOrigin; } void SetOrigin(const WorldPt& origin) { fOrigin = origin; } private: WorldPt fOrigin; };
What’s next? Look at the “spec” in Episode 0, it looks like the front tiles are made of rows and columns – but let’s not jump to conclusions yet. Dr. Bob is just a quack, not a CS major. The next simple test which comes to our mind is checking for the position of the second front tile – which should be located to the right of the first front tile (we assume that both tiles fit into the cabinet, of course).
TEST_N(FrontModelPositionOfSecondSegment) { SimpleCabinetModel simpleCabinet(600, 720, 800); FrontModel frontModel; simpleCabinet.GetFrontModel(frontModel); CHECK_EQUAL(frontModel.GetOrigin() + WorldPt(SimpleCabinetModel::kSegmentWidth, 0), frontModel.GetPositionOfSecondSegment()); }
In order to calculate the second tiles position, we need the width of a front tile. For now, we’re accessing SimpleCabinetModel::kSegmentWidth from FrontModel::GetPositionOfSecondSegment() for this purpose.
WorldPt GetPositionOfSecondSegment() const { return fOrigin + WorldPt(SimpleCabinetModel::kSegmentWidth, 0); }
Doesn’t feel absolutely right yet, but we’re confident that this will be smoothed out over the next tests & episodes.
After the raving success with SimpleCabinetCreator in Episode 4, let’s see if we can create the Vectorworks geometry for the first & second tile before continuing our journey fleshing out the FrontModel. We have the uneasy feeling that The reason why
Let’s start with the plug-in object’s Recalculate() function. We spice up SimpleCabinetCreator a bit, as we need access to both the SimpleCabinetModel and FrontModel. Asking SimpleCabinetModel to “fill in the blanks” in FrontModel didn’t feel right, so let’s change it and pass the origin as a parameter to FrontModel‘s constructor. Way better.
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); FrontModel frontModel(simpleCabinetModel.GetFrontModelOrigin()); SimpleCabinetCreator creator(object); creator.CreateFrom(simpleCabinetModel, frontModel); return kObjectEventNoErr; }
Where does SimpleCabinetCreator get the front symbols name from? FrontModel, of course. We hard-wire the name of the symbol for now:
class SimpleCabinetCreator { public: SimpleCabinetCreator(const VWParametricObj& pio) : fSimpleCabinet(pio) { } void CreateFrom(const SimpleCabinetModel& cabinetModel, const FrontModel& frontModel) { CreateFrom(cabinetModel); CreateFrom(frontModel); } private: void CreateFrom(const SimpleCabinetModel& cabinetModel) { // ... } void CreateFrom(const FrontModel& frontModel) { MCObjectHandle firstFrontSegment = gSDK->PlaceSymbolByNameN(frontModel.GetFrontSymbolName(), frontModel.GetOrigin()); fSimpleCabinet.AddObject(firstFrontSegment); MCObjectHandle secondFrontSegment = gSDK->PlaceSymbolByNameN(frontModel.GetFrontSymbolName(), frontModel.GetPositionOfSecondSegment()); fSimpleCabinet.AddObject(secondFrontSegment); } VWParametricObj fSimpleCabinet; };
Firing up Vectorworks 2011, we are supposed to be – once again – pleased with the results. However, a closer inspection reveals that the front tiles collide with the bottom board – ah, we forgot the tiles z position in our model.
Tune in next week when you heard Dr. Bob say: “Nurse Piggy, would you mind slapping this developer here with a rolled-up newspaper?”
[…] Episode 5 – A third class […]
[…] Previous Episode | Next Episode […]
[…] left off in Episode 5 trying to avoid Dr. Bob hitting us with a rolled-up newspaper as we forgot to account for the front […]
[…] Episode 5 – A third class […]