Set different layers for different objects in a script

Use this forum to ask questions about how to do things in QCAD.

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files and screenshots.

Post one question per topic.

Post Reply
PeterR
Junior Member
Posts: 10
Joined: Thu Oct 21, 2010 7:34 pm

Set different layers for different objects in a script

Post by PeterR » Fri Jan 30, 2015 2:46 pm

Hello,

I work with Xubuntu 14.04 (64 bit) and QCad Professional 3.7.0. The script BlocksFromFile reads lines from a csv-file and created blocks from the data stored in this file. It is used to start in a drawing, which was created by a template, where a few layers are defined.
The blocks consists of a rectangle and two numbers, which describe the length and the width. Layer “18 mm” is to be assigned to the rectangle (the name of the layer depends on the data in the csv-file). Layer “Text” is to be assigned to the numbers.
…
		var block = new RBlock(document, name, new RVector(0,0));
		var operation = new RAddObjectOperation(block);
		di.applyOperation(operation);
		// make new block the current block:
		di.setCurrentBlock(name);
		// Layer festlegen
		switch (stueck[6]) {
			case 18:
				ebene = "18 mm";
				break;	
			case 28:
				ebene = "28 mm";
				break;
			case 5:
				ebene = "5 mm";
				break;
			default:
				ebene = "0";
				break;
		}
		di.setCurrentLayer(ebene);
		// Zeichne ein Rechteck aus Länge und Breite
		var p1 = new RVector(0, 0);
		var p2 = new RVector(stueck[2], 0);
		var p3 = new RVector(stueck[2], stueck[3]);
		var p4 = new RVector(0, stueck[3]);
		var line1 = new RLineEntity(document, new RLineData(p1, p2));
		var line2 = new RLineEntity(document, new RLineData(p2, p3));
		var line3 = new RLineEntity(document, new RLineData(p3, p4));
		var line4 = new RLineEntity(document, new RLineData(p4, p1));
		operation = new RAddObjectsOperation();
		operation.addObject(line1);
		operation.addObject(line2);
		operation.addObject(line3);
		operation.addObject(line4);
		
		// Layer festlegen
		di.setCurrentLayer("Text");
		// Länge und Breite als Text in die Mitte der Seiten
		var pos = new RVector((stueck[2]/2),stueck[3]);
		// create a text entity and add it to the operation:
		// für die Länge
		var textL = new RTextEntity(
			document,
			new RTextData(
              pos,                 // position
              pos,                 // alignment point
              20,                 // height
              20,                 // text width (ignored for now)
              RS.VAlignTop,        // alignments
              RS.HAlignCenter,
              RS.LeftToRight,
              RS.Exact,
              1.0,                 // line spacing factor
              String(stueck[2]),              // the text
              "Liberation Sans",      // font
              false,        // bold
              false,        // italic
              0.0,          // angle
              false         // simple text without formatting
			)
		);
		operation.addObject(textL);
		// Berechne die Position
		var pos = new RVector(0,(stueck[3]/2));
		// für die Breite
		var textB = new RTextEntity(
			document,
			new RTextData(
              pos,                 // position
              pos,                 // alignment point
              20,                 // height
              20,                 // text width (ignored for now)
              RS.VAlignMiddle,        // alignments
              RS.HAlignLeft,
              RS.LeftToRight,
              RS.Exact,
              1.0,                 // line spacing factor
              String(stueck[3]),           // the text
              "Liberation Sans",      // font
              false,        // bold
              false,        // italic
              0.0,          // angle
              false         // simple text without formatting
			)
		);
		operation.addObject(textB);
				
		di.applyOperation(operation);
...
In this case the complete block is assigned to layer “Text”. When I put the the statement di.applyOperation(operation); after the drawing of the rectangle (operation.addObject(line4);) the application will crash.
In the attachment you will find the complete coding of the script. How must I change the coding, so that there are different layers for different objects?

Regards

Peter
Attachments
BlocksFromFile.js
The script is embedded in the (german) menu: Diverses -> Block -> Blöcke aus csv-Datei
(8.41 KiB) Downloaded 343 times

User avatar
andrew
Site Admin
Posts: 9059
Joined: Fri Mar 30, 2007 6:07 am

Re: Set different layers for different objects in a script

Post by andrew » Fri Jan 30, 2015 3:04 pm

operation.addObject(obj); adds entities to the current layer with current (user defined) attributes by default.
Please use operation.addObject(obj, false); to use the layer ID, block ID and attributes stored in obj instead.

PeterR
Junior Member
Posts: 10
Joined: Thu Oct 21, 2010 7:34 pm

Re: Set different layers for different objects in a script

Post by PeterR » Fri Jan 30, 2015 4:40 pm

Sorry, I tried to realize your answer. But it did not work. Mostly I got syntax errors or nothing happened. Can you give me an example?

User avatar
andrew
Site Admin
Posts: 9059
Joined: Fri Mar 30, 2007 6:07 am

Re: Set different layers for different objects in a script

Post by andrew » Fri Jan 30, 2015 4:48 pm

Change line
operation.addObject(textL);
to:
operation.addObject(textL, false);

PeterR
Junior Member
Posts: 10
Joined: Thu Oct 21, 2010 7:34 pm

Re: Set different layers for different objects in a script

Post by PeterR » Fri Jan 30, 2015 7:37 pm

Code: Select all

operation.addObject(textL, false);
This was the first action I have done. But it did not solve the problem. The layer of the complete block is "Text".

So I tried to add the layer of the lines.

Code: Select all

var layrectId = document.getCurrentLayerId();
var layrect = document.queryLayer(layrectId);
operation.addObject(layrect);
But this causes an error. Is this the right way?

User avatar
andrew
Site Admin
Posts: 9059
Joined: Fri Mar 30, 2007 6:07 am

Re: Set different layers for different objects in a script

Post by andrew » Fri Jan 30, 2015 7:45 pm

Could it be that you creating the block reference (RBlockReference) on layer 'Text'?
Note that a block reference is an entity and as such on a layer. Each entity inside a block is also on an individual layer.

PeterR
Junior Member
Posts: 10
Joined: Thu Oct 21, 2010 7:34 pm

Re: Set different layers for different objects in a script

Post by PeterR » Tue Feb 10, 2015 2:34 pm

No, there was no block reference. I tried and looked for examples and now I solved the problem.

I added/changed the following lines:

Code: Select all

line1.setLayerId(document.getLayerId(layername));
line2.setLayerId(document.getLayerId(layername));
line3.setLayerId(document.getLayerId(layername));
line4.setLayerId(document.getLayerId(layername));
operation = new RAddObjectsOperation();
operation.addObject(line1,false);
operation.addObject(line2,false);
operation.addObject(line3,false);
operation.addObject(line4,false);
The "Text" entities has been changed the same way:

Code: Select all

textL.setLayerId(document.getLayerId(layername));
operation.addObject(textL,false);
Also I added coding, which creates the necessary folders. Therefore you don't have to use a template.
The complete code is attached.
Attachments
BlocksFromFile.js
changed file
(9.12 KiB) Downloaded 341 times

PeterR
Junior Member
Posts: 10
Joined: Thu Oct 21, 2010 7:34 pm

Re: Set different layers for different objects in a script

Post by PeterR » Wed Feb 11, 2015 2:14 pm

Sorry, there is a wrong coding: when block names exists, the result is not correct.
This coding must be written before block is created.

Code: Select all

// existiert ein Block mit dem Namen name?
var blocknames = document.getBlockNames();
blocknames.sort();
j=0;	
for (var i=0; i<blocknames.length; i++) {
	if (name == blocknames[i]) {
		j++;
		name = name + " " + j;
	}
}
// create new block definition:

Post Reply

Return to “QCAD 'How Do I' Questions”