AddForSolid() In C++

AddForSolid() In C++

Anonymous
Not applicable
1,134 Views
6 Replies
Message 1 of 7

AddForSolid() In C++

Anonymous
Not applicable

Hi there,

 

I am trying to creating an extrusion with a sketch containing multiple circles. However, the AddForSolid() Method is not working properly, returning non-NOERROR is C++. 

 

Below is the code that I used to create a sketch(which is succeeded) and then try to create a extrusion (in my case cutting). 

	ApplicationPtr pInvApp;
	pInvApp.GetActiveObject("Inventor.Application");
	DocumentPtr pItem;
	pItem = pInvApp->GetActiveDocument();
	PartDocumentPtr pDoc;
	if(pItem->GetDocumentType() == kPartDocumentObject)
		pDoc = pItem;
	SelectSetPtr selectedItem;
	selectedItem = pDoc->GetSelectSet();
	FacePtr face;
	//printf("\n*************Face Selection***************\n");
	face = selectedItem->GetItem(1);
	if(face->GetType() == kFaceObject)
		printf("You have selected a face\n");
	WorkPlanePtr pWorkPlane; 
	tagVARIANT offset;
	offset.vt = sizeof(long);
	offset.lVal = 0;
	if(pDoc->ComponentDefinition->WorkPlanes->AddByPlaneAndOffset(face, offset, 0, &pWorkPlane) == NOERROR)
		printf("Succeeded to set the workplane!\n");

	//Create a sketch based on the work plane just selected and set the transient geometry
	PlanarSketchPtr pSketch;
	if(pDoc->ComponentDefinition->Sketches->Add(pWorkPlane, false, &pSketch) == NOERROR)
		printf("Suceeded to set the sketch!\n");

	TransientGeometryPtr pTG;
	pTG = pInvApp->GetTransientGeometry();

	PartComponentDefinitionPtr pCompDef;
	pCompDef = pDoc->GetComponentDefinition();
	Point2dPtr point;
	int i, j;
	int m = 2;
	int n = 2;
	for(i = 0; i < m; i++)
	{
		for(j = 0; j < n; j++)
		{
			pInvApp->TransientGeometry->CreatePoint2d(i*0.3, j*0.3, &point);

			//Sketch a circle using the point defined and the radius
			SketchCirclePtr pCircle;
			pSketch->SketchCircles->AddByCenterRadius(point, 0.1, &pCircle);
			//printf("%ld\n",pSketch->SketchEntities->GetCount());
		}
	}
	
	//Create new profile
	ProfilePtr pProfile;
	tagVARIANT profilePathSegments;
	tagVARIANT reserved;
	//Fail to create profile
	if(pSketch->Profiles->AddForSolid(false, profilePathSegments, reserved,  &pProfile) != NOERROR)
	{
		printf("Fail to create profile!(AddForSolid)\n");
		return E_FAIL;
	}

	//Create a new Extrution definition method
	ExtrudeDefinitionPtr pExtrudeDef;
	
	if(pCompDef->Features->ExtrudeFeatures->CreateExtrudeDefinition(pProfile, kCutOperation, &pExtrudeDef) != NOERROR)
	{
		printf("Fail to create extrude definition\n!");
		return E_FAIL;
	}

	VARIANT distance;
	distance.vt = sizeof(double);
	distance.dblVal = 1.0;
	
	if(pExtrudeDef->SetDistanceExtent(distance, kSymmetricExtentDirection) != NOERROR)
		printf("Set Extrude Extent Fail\n");
	else
		printf("Extrution Feature Set!\n");
	
	//Do the extrusion
	ExtrudeFeaturePtr pExtrude;
	pCompDef->Features->ExtrudeFeatures->Add(pExtrudeDef, &pExtrude);
	
	return 0;

}

 

0 Likes
1,135 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable

The code is returning on error in the following line:

  //Create new profile
  ProfilePtr pProfile;
  tagVARIANT profilePathSegments;
  tagVARIANT reserved;
  //Fail to create profile
  if(pSketch->Profiles->AddForSolid(false, profilePathSegments, reserved,  &pProfile) != NOERROR)
  {
	printf("Fail to create profile!(AddForSolid)\n");
	return E_FAIL;
  }

 is there anything wrong with it? The pSketch is the sketch that I created previous to this part. 

 

Thank you.

 

0 Likes
Message 3 of 7

Anonymous
Not applicable

And, could anyone show me a example of how to create an extrusion with C++ based on a sketch of multiple entities? 

 

Many thanks

 

Difan

0 Likes
Message 4 of 7

xiaodong_liang
Autodesk Support
Autodesk Support

Hi,

 

Please refer to SDK sample:

 

C:\Users\Public\Documents\Autodesk\Inventor 2013\SDK\DeveloperTools\Samples\VC++\AddIns\CustomCommand

Message 5 of 7

Anonymous
Not applicable

Xiaodong,

 

Thank you a lot for your prompt reply. I have tried that SDK sample, but I can not compile it sucessfully.

 

The error message is: rackfacecmd.cpp(452): error C2664: 'UnitsOfMeasure::GetValueFromExpression' : cannot convert parameter 3 from 'double *' to 'VARIANT *'

 

The datatype VARIANT is also making me headache when I am programming with Inventor API in C++. Could you tell me some thing about how to use VARIANT properly (to me the VARIANT datatype is a union which could be any basic datatype)?

 

Thank you!

 

Sincerely,

Difan

 

 

0 Likes
Message 6 of 7

Anonymous
Not applicable

And, I think I still need to know about how to make extrusion happen in C++....

 

Thank you a lot.

0 Likes
Message 7 of 7

xiaodong_liang
Autodesk Support
Autodesk Support

Hi DifanH,

 

Sorry for my delay.

 

The following is a code on how to add an extrude feature in C++. Hope it helps.

 

Yes, Variant is a Variant data type is a tagged union that can be used to represent any other data type.  I have not a detailed description how to use or to say how to use with Inventor API. Its usage is very flexible. I'd suggest you take a look at some posts on internet. If you hit any problem, please post in forum.

 

static HRESULT addsolid()
{

	HRESULT hr = NOERROR;

	   CLSID InvAppClsid;
  hr = CLSIDFromProgID (L"Inventor.Application", &InvAppClsid);
  if (FAILED(hr)) return hr;
 
  CComPtr<IUnknown> pInvAppUnk;
  hr = ::GetActiveObject (InvAppClsid, NULL, &pInvAppUnk);
  if (FAILED (hr))
    _tprintf_s(_T("*** Could not get hold of an active Inventor application ***\n"));
  if (FAILED(hr)) return hr;

    CComPtr<Application> pInvApp;
  hr = pInvAppUnk->QueryInterface(
    __uuidof(Application), (void **) &pInvApp);
  if (FAILED(hr)) return hr;

  CComPtr<Document>	piDoc;
	  hr = pInvApp->get_ActiveDocument(&piDoc); 
	  if (FAILED(hr)) return hr;

	CComQIPtr<PartDocument> pPartDoc(piDoc);

	CComPtr<PartComponentDefinition> piPartCompDef;
	hr = pPartDoc->get_ComponentDefinition(&piPartCompDef);
	 if (FAILED(hr)) return hr;
 

	 	CComPtr<TransientGeometry> oTG;
	hr = pInvApp->get_TransientGeometry(&oTG);
	if (FAILED(hr)) return hr;

	CComPtr<Point2d> oCenterP;
	hr = oTG->CreatePoint2d(0, 0, &oCenterP);
	if (FAILED(hr)) return hr;

	  CComPtr<PlanarSketch> pSketch = NULL;
	hr = piPartCompDef->Sketches->get_Item(CComVariant(_T("Sketch1")), &pSketch);
	 if (FAILED(hr)) return hr;

	   CComPtr<SketchCircle> pSketchCircle = NULL;
	hr = pSketch->SketchCircles->AddByCenterRadius(oCenterP,2);
	 if (FAILED(hr)) return hr;

	 
	   CComPtr<Profile> pProfile = NULL;
	   CComVariant pSegs;
	   CComVariant preserve;
	hr = pSketch->Profiles->AddForSolid(VARIANT_TRUE,pSegs,preserve,&pProfile);
	 if (FAILED(hr)) return hr;



	 CComPtr<PartFeatures> pPartFs;
	 hr = piPartCompDef->get_Features(&pPartFs);
	 if (FAILED(hr)) return hr;

	 CComPtr<ExtrudeFeatures> pExtrudeFs;
	 hr = pPartFs->get_ExtrudeFeatures(&pExtrudeFs);
	 if (FAILED(hr)) return hr;

	  CComPtr<ExtrudeDefinition> pExtrudeDef;
	 hr = pExtrudeFs->CreateExtrudeDefinition(pProfile,PartFeatureOperationEnum::kJoinOperation,&pExtrudeDef);
	 if (FAILED(hr)) return hr;

	 pExtrudeDef->SetDistanceExtent(_variant_t(2.0),PartFeatureExtentDirectionEnum::kPositiveExtentDirection );

	 CComPtr<ExtrudeFeature> pExtrude;
	 	 hr = pExtrudeFs->Add (pExtrudeDef);
		 if (FAILED(hr)) return hr;



   return hr;
}

 

 

0 Likes