Create constraint by DatumAxis in assembly

I'm new to NX
I have two parts in the assembly and want to create assembly constraint by the axes Z (DatumAxis) of each component. Constraint created successfully, but there are problems. When you try to edit constraint using UI the alert:
“Some references in the constraint are invalid and cannot be retained in a redefine”
And if you make one part as Work Part, it goes away from the related part.

Could you please help me ?
Thanks.

(Sorry for my english...)


void makeConstraintByDatumAxis()
{
tag_t tElem1=selObjectBySolid();
tag_t tElem2=selObjectBySolid();

if (tElem1 && tElem2)
{
tag_t pObjPrototype1(0),pTopOcc1(0),pCurLevelOcc1(0);
GetSelectedObjectData(tElem1, &pObjPrototype1, &pTopOcc1, &pCurLevelOcc1);

tag_t pObjPrototype2(0),pTopOcc2(0),pCurLevelOcc2(0);
GetSelectedObjectData(tElem2, &pObjPrototype2, &pTopOcc2, &pCurLevelOcc2);

NXOpen::Assemblies::Component *oComp1,*oComp2;
tag_t ax1,ax2;

if (pTopOcc1 && pTopOcc2)
{
oComp1 = dynamic_cast(NXOpen::NXObjectManager::Get(pTopOcc1));
oComp2 = dynamic_cast(NXOpen::NXObjectManager::Get(pTopOcc2));

ax1=getDatumAxisZ(oComp1);
ax2=getDatumAxisZ(oComp2);
}
if (tElem1 && tElem2)
{
NXOpen::Assemblies::ComponentAssembly * oBaseCompAss;
oBaseCompAss=oComp1->DirectOwner();
NXOpen::Positioning::ComponentPositioner * pPositioner = oBaseCompAss->Positioner();

//------------------------------------------------------------------------------------------------------PART1
NXOpen::NXObject *geomObj1=dynamic_cast(NXOpen::NXObjectManager::Get(ax1));
NXOpen::NXObject *moveObj1=dynamic_cast(NXOpen::NXObjectManager::Get(oComp1->Tag()));
//------------------------------------------------------------------------------------------------------PART2
NXOpen::NXObject *geomObj2=dynamic_cast(NXOpen::NXObjectManager::Get(ax2));
NXOpen::NXObject *moveObj2=dynamic_cast(NXOpen::NXObjectManager::Get(oComp2->Tag()));

pPositioner->ClearNetwork();
pPositioner->BeginAssemblyConstraints();
NXOpen::Positioning::ComponentNetwork *pComponentNetwork = dynamic_cast(pPositioner->EstablishNetwork());

pComponentNetwork->SetMoveObjectsState(true);
NXOpen::Positioning::Constraint * pNewConstraint = pPositioner->CreateConstraint();

NXOpen::Positioning::Constraint::Alignment ConstrAlignment = NXOpen::Positioning::Constraint::AlignmentContraAlign;
NXOpen::Positioning::Constraint::Type ConstrType = NXOpen::Positioning::Constraint::TypeTouch;
pNewConstraint->SetConstraintAlignment(ConstrAlignment);
pNewConstraint->SetConstraintType(ConstrType);
pNewConstraint->SetAutomatic(false);

NXOpen::Positioning::ConstraintReference *pRef1 = pNewConstraint->CreateConstraintReference(moveObj1,geomObj1,false,false,false);
NXOpen::Positioning::ConstraintReference *pRef2 = pNewConstraint->CreateConstraintReference(moveObj2,geomObj2,false,false,false);

pComponentNetwork->Solve();
pComponentNetwork->ResetDisplay();
pComponentNetwork->ApplyToModel();

pPositioner->ClearNetwork();
pPositioner->EndAssemblyConstraints();
}
}
}
tag_t getDatumAxisZ ( NXOpen::Assemblies::Component *pComp )
{
const char *utf1;
const char *utf3;
CString strN0, strN1, strAxis1, strAxis2, strPt1, strPt2;

NXOpen::NXString name;

NXOpen::BasePart *bsPartAss=theSession->Parts()->Work();

NXOpen::INXObject *obj=pComp->Prototype();
NXOpen::BasePart *bsPart=obj->OwningPart();

theSession->Parts()->SetWork(bsPart);
//NXOpen::BasePart *part=bsPart->OwningPart();

NXOpen::Assemblies::ComponentAssembly *oCompAss = bsPart->ComponentAssembly();
NXOpen::DatumCollection *datColl=bsPart->Datums();
NXOpen::DatumCollection::iterator it3;

for(it3=datColl->begin();it3!=datColl->end();it3++)
{
NXOpen::DatumAxis *datAxis1=NULL;
datAxis1 = dynamic_cast(*it3);
if (datAxis1)
{
NXOpen::NXString nxStr=datAxis1->JournalIdentifier();
utf1=nxStr.GetUTF8Text();
strN1=utf1;

if (strN1.Find("DATUM_CSYS(0) Z axis")>=0)
{
tagObj1=datAxis1->Tag();
}
}
}
theSession->Parts()->SetWork(bsPartAss);

return tagObj1;
}
tag_t selObjectBySolid ( void )
{
NXOpen::Point3d curs;
NXOpen::Selection::SelectionScope scope = Selection::SelectionScopeAnyInAssembly;//:SelectionScopeWorkPartAndOccurrence;
NXOpen::Selection::SelectionAction action = Selection::SelectionActionClearAndEnableSpecific;
std::vectorselectionMask_array(1);

selectionMask_array[0].Type = UF_solid_type;

selectionMask_array[0].Subtype = UF_all_subtype; //UF_sketch_ref_line_subtype;
const char* title="Select ";
const char* messg="Select solid";
selectionMask_array[0].SolidBodySubtype = UF_UI_SEL_FEATURE_SOLID_BODY;

std::vectorselectionType_array(1);
selectionType_array[0].Type = UF_UI_SEL_TYPE_SINGLE_SELECTION;//SelectionTypeCurves;
//---------------------------------------------------------------------------------------------------------
curs.X = 0.0; curs.Y = 0.0; curs.Z = 0.0;
tag_t tSelObj=0;
NXOpen::NXObject *oObject1=NULL;
theUI->SelectionManager()->SelectObject(messg, title, scope, action, false, false, selectionMask_array, &oObject1, &curs);
if (oObject1)
tSelObj=oObject1->Tag();

return tSelObj;
}
bool GetSelectedObjectData( tag_t selObject, tag_t *pObjectPrototype, tag_t *pTopOcc, tag_t *pCurLevelOcc )
{
if(!selObject)
return false;

*pCurLevelOcc = 0;
tag_t tTopOcc = UF_ASSEM_ask_part_occurrence(selObject);

if(tTopOcc)
{
tag_t tProtoObject = UF_ASSEM_ask_prototype_of_occ(selObject);
if(UF_OBJ_is_object_a_promotion (tProtoObject) )
{

{
{
tTopOcc = UF_ASSEM_ask_work_occurrence();
if(tTopOcc!=0)
selObject = tProtoObject;
}
}
}
else
selObject = tProtoObject;
}

tag_t tWorkOccurrence = UF_ASSEM_ask_work_occurrence();
if( (tWorkOccurrence == tTopOcc) && (tWorkOccurrence == 0) )
{
*pCurLevelOcc = 0;
}
else if(tWorkOccurrence)
{
tag_t tPrototypeWorkOcc = UF_ASSEM_ask_prototype_of_occ(tWorkOccurrence);
tag_t tRootPartOcc = UF_ASSEM_ask_root_part_occ(tPrototypeWorkOcc);

tag_t tInstance = UF_ASSEM_ask_inst_of_part_occ(tTopOcc);
tag_t *part_occs=NULL;
long iCount= UF_ASSEM_ask_part_occs_of_inst(tInstance,&part_occs);

for(long i=0;i {
tag_t tPartOwner = part_occs[i];
while(tPartOwner)
{
if(tPartOwner == tRootPartOcc )
{
*pCurLevelOcc = part_occs[i];
break;
}
tPartOwner = UF_ASSEM_ask_part_occurrence(tPartOwner);
}

}
UF_free(part_occs);
}
else
*pCurLevelOcc = tTopOcc;

*pTopOcc = tTopOcc;
*pObjectPrototype = selObject;

return true;
}