We create a sketch calling SolidWorks Sketch APIs and add dimension.
For example -
we create x-y plane using API IModelDoc2::ICreatePlaneFixed2()
We create line (10,10,0) (20,20,0) using API ISketchManager::CreateLine()
We add dimension constraint to line using.
The API code called is as follows.
================================
IModelDoc2* iModelDoc2 = 0;
BSTR retvalstr;
m_iSwApp->GetUserPreferenceStringValue(swDefaultTemplatePart, &retvalstr);
m_iSwApp->INewDocument2(retvalstr, swDwgPaperA2size, 0.0, 0.0, &iModelDoc2);
double origin[3], Xaxis[3], Yaxis[3];
origin[0] = origin[1] = origin[2] = 0.0;
Xaxis[0] = 0.05;
Xaxis[1] = Xaxis[2] = 0.0;
Yaxis[0] = Yaxis[2] = 0.0;
Yaxis[1] = 0.05;
IRefPlane* ouputplane = 0;
iModelDoc2->ICreatePlaneFixed2(origin, Xaxis, Yaxis, FALSE, &ouputplane);
BSTR planeName;
iModelDoc2->IGetEntityName((IEntity*)ouputplane, &planeName);
IModelDocExtension* docExt = 0;
iModelDoc2->get_Extension(&docExt);
VARIANT_BOOL retval;
CComBSTR sPlane(L"PLANE");
docExt->SelectByID2(planeName, sPlane, 0, 0, 0, false, 0, NULL, swSelectOptionDefault, &retval);
iModelDoc2->InsertSketch2(true);
iModelDoc2->ClearSelection2(true);
ISketchManager* sketchManager = 0;
iModelDoc2->get_SketchManager(&sketchManager);
ISketchSegment* outputSketch = 0;
sketchManager->CreateLine(0.01, 0.01, 0.0, 0.04, 0.04, 0.0, &outputSketch);
ISketchLine *pSketchLine = NULL;
outputSketch->QueryInterface(IID_ISketchLine, (LPVOID *)&pSketchLine);
ISketchPoint* lineStartPt = 0;
ISketchPoint* lineEndPt = 0;
pSketchLine->IGetStartPoint2(&lineStartPt);
pSketchLine->IGetEndPoint2(&lineEndPt);
ISelectionMgr* selMgr = 0;
iModelDoc2->get_ISelectionManager(&selMgr);
CComQIPtr
selMgr->CreateSelectData(&swSelData);
long mark = 1;
swSelData->put_Mark(mark);
VARIANT_BOOL retVal = FALSE;
lineStartPt->Select4(TRUE, swSelData, &retVal);
lineEndPt->Select4(TRUE, swSelData, &retVal);
swUserPreferenceToggle_e userPreferenceToggle = swInputDimValOnCreate;
m_iSwApp->SetUserPreferenceToggle(userPreferenceToggle, FALSE);
IDisplayDimension* displayDim = 0;
iModelDoc2->IAddHorizontalDimension2(0.0, 0.0, 0.0, &displayDim); // We do not know dimension location
m_iSwApp->SetUserPreferenceToggle(userPreferenceToggle, TRUE);
VARIANT_BOOL deSel = FALSE;
lineStartPt->DeSelect(&deSel);
lineEndPt->DeSelect(&deSel);
iModelDoc2->ClearSelection2(true);
================================
As expected, the dimension label is seen at origin: Please see snapshot DimLabelAtOrigin.jpg
My question:
Is there any API which automatically aligns the dimension label in a sketch at appropriate location? This API should compute appropriate dimension label location internally. By using this API, we can align the labels appropriately even if they are located at origin initially using IAddHorizontalDimension2 or IAddDimension2 etc.
SolidworksApi macros