script listAndCountBlocks
Posted: Sat Mar 08, 2025 2:05 am
Hello!
I'm using chatopenai to create some scripts for QCad, as I know nothing about js language, and the result is amazing.
Look that, it lists and count all blocks in a draw:
function listAndCountBlocks() {
var appWin = RMainWindow.getMainWindow();
if (!appWin) {
qDebug("Erro: não foi possível obter a janela principal.");
return;
}
var doc = EAction.getDocument();
var di = EAction.getDocumentInterface();
if (!doc || !di) {
qDebug("Erro: não foi possível obter o documento ou a interface do documento.");
return;
}
var ids = doc.queryAllEntities();
var blockCounts = {};
for (var i = 0; i < ids.length; i++) {
var entity = doc.queryEntity(ids);
// Verifica se a entidade é uma inserção de bloco
if (isBlockReferenceEntity(entity)) {
var blockName = entity.getReferencedBlockName();
if (blockName in blockCounts) {
blockCounts[blockName]++;
} else {
blockCounts[blockName] = 1;
}
}
}
// Criar mensagem com os blocos e suas quantidades
var message = "Lista de blocos inseridos no desenho:\n";
for (var block in blockCounts) {
message += "Bloco: " + block + " - Quantidade: " + blockCounts[block] + "\n";
}
// Exibir a mensagem
qDebug(message);
QMessageBox.information(appWin, "Blocos no Desenho", message);
}
// Chama a função para listar e quantificar todos os blocos inseridos
listAndCountBlocks();
I'm using chatopenai to create some scripts for QCad, as I know nothing about js language, and the result is amazing.
Look that, it lists and count all blocks in a draw:
function listAndCountBlocks() {
var appWin = RMainWindow.getMainWindow();
if (!appWin) {
qDebug("Erro: não foi possível obter a janela principal.");
return;
}
var doc = EAction.getDocument();
var di = EAction.getDocumentInterface();
if (!doc || !di) {
qDebug("Erro: não foi possível obter o documento ou a interface do documento.");
return;
}
var ids = doc.queryAllEntities();
var blockCounts = {};
for (var i = 0; i < ids.length; i++) {
var entity = doc.queryEntity(ids);
// Verifica se a entidade é uma inserção de bloco
if (isBlockReferenceEntity(entity)) {
var blockName = entity.getReferencedBlockName();
if (blockName in blockCounts) {
blockCounts[blockName]++;
} else {
blockCounts[blockName] = 1;
}
}
}
// Criar mensagem com os blocos e suas quantidades
var message = "Lista de blocos inseridos no desenho:\n";
for (var block in blockCounts) {
message += "Bloco: " + block + " - Quantidade: " + blockCounts[block] + "\n";
}
// Exibir a mensagem
qDebug(message);
QMessageBox.information(appWin, "Blocos no Desenho", message);
}
// Chama a função para listar e quantificar todos os blocos inseridos
listAndCountBlocks();