В зависимости от того какой тип документа открыт, необходимо выполнить процедуру.
В SDK нашел пример на текущий документ, но что-то не работает. Где ошибка?
0 - любой документ
1 -
2 -
procedure LIBRARYENTRY(command: WORD); pascal;
begin
iKompas := KompasObject(CreateKompasObject);
iDocumentParam:=ksDocumentParam(iKompas.GetParamStruct(ko_DocumentParam));
pDoc := ksGetCurrentDocument(0);
if ( pDoc = 1 ) then
begin
ВЫПОЛНИТЬ!;
end
else
if ( pDoc = 2 ) then
begin
ВЫПОЛНИТЬ!;
end;
Данная функция, возвращает указатель, а не тип документа, поэтому и не работает.
uses
Windows,
SysUtils,
Contnrs,
Ldefin2D,
LibTool,
LtDefine,
ksAuto,
KsTLB,
ksAPI7,
ksConstTLB,
LDefin3D;
interface
type
TDocEntitySet7 = set of ksDocumentUnknown..ksDocumentTextual;
var
iKompasO : KompasObject;
iDoc3D: ksDocument3D; //Интерфейс документа модели
lIDoc3D: IKompasDocument3D;
lIPart: IPart7;
iKApp: IApplication; //Интерфейс КОМПАС API 7
function fnCreateKompasObject : boolean;
function CurrentDocumentTypeIs7(_KApp: IApplication; KompasDocTypeSet:
TDocEntitySet7): Boolean;
implementation
function fnCreateKompasObject : boolean;
Var
disp: IDispatch;
begin
result := false;
try
iKompasO := KompasObject(CreateKompasObject);
if iKompasO <> nil then
iDoc3D := ksDocument3D(iKompasO.ActiveDocument3D);
result := (iKompasO <> nil) and (iDoc3D <> nil);
if result then
begin
if iKApp = nil then
begin
disp := IDispatch(CreateKompasApplication);
if disp <> nil then
iKApp := disp as IApplication;
result := (iKApp <> nil);
lIDoc3D := iKApp.ActiveDocument as IKompasDocument3D;
lIPart := lIDoc3D.TopPart;
end;
end;
except
end;
end;
function CurrentDocumentTypeIs7(_KApp: IApplication; KompasDocTypeSet:
TDocEntitySet7): Boolean;
begin
Result := False;
if (_KApp <> nil) and (_KApp.ActiveDocument <> nil) then
if _KApp.ActiveDocument.DocumentType in KompasDocTypeSet then
Result := True;
end;
//=================
Пример использования
fnCreateKompasObject;
if CurrentDocumentTypeIs7(iKApp, [ksDocumentAssembly]) then
................
На Python:
iDocument = iApplication.ActiveDocument # получить интерфейс активного документа
Type = iDocument.DocumentType # получить тип документа
В переменную Type записывается целое число, соответствующее типу документа.
Типы документов КОМПАС:
ksDocumentUnknown 0 Неизвестный тип
ksDocumentDrawing 1 Чертеж
ksDocumentFragment 2 Фрагмент
ksDocumentSpecification 3 Спецификация
ksDocumentPart 4 Деталь
ksDocumentAssembly 5 Сборка
ksDocumentTextual 6 Текстовый документ
ksDocumentTechnologyAssembly 7 Технологическая сборка
Сделал так. Всем спасибо!
if (iKompas.ksGetDocumentType(0) = lt_DocSheetStandart) then
if (iKompas.ksGetDocumentType(0) = lt_DocSpc) then