Skip to content

Commit

Permalink
Merge pull request enjoping#11 from level-level/add-lwpolyline-parsin…
Browse files Browse the repository at this point in the history
…g-support

Add support for parsing LWPolyline entities
  • Loading branch information
atimmer authored Jan 21, 2021
2 parents 25d9867 + c186fae commit 183c307
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
26 changes: 24 additions & 2 deletions DXFighter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use DXFighter\lib\Ellipse;
use DXFighter\lib\Insert;
use DXFighter\lib\Line;
use DXFighter\lib\LWPolyline;
use DXFighter\lib\Polyline;
use DXFighter\lib\Spline;
use DXFighter\lib\Text;
Expand Down Expand Up @@ -454,11 +455,15 @@ private function readEntitiesSection($values, $addEntities = false, $move = [0,0
$entities = [];
$entityType = '';
$data = [];
$types = ['TEXT', 'LINE', 'ELLIPSE', 'SPLINE', 'INSERT', 'ARC', 'CIRCLE'];
$types = ['TEXT', 'LINE', 'ELLIPSE', 'SPLINE', 'INSERT', 'ARC', 'CIRCLE', 'LWPOLYLINE'];
// TODO most entity types are still missing
foreach ($values as $value) {
if ($value['key'] == 0) {
if ((in_array($entityType, $types) && !empty($data)) || in_array($entityType, ['POLYLINE', 'VERTEX']) && $value['value'] == 'SEQEND') {
// This condition happens at the end of an entity
if (
(in_array($entityType, $types) && !empty($data)) ||
in_array($entityType, ['POLYLINE', 'VERTEX']) && $value['value'] == 'SEQEND'
) {
$entity = $this->addReadEntity($entityType, $data, $move, $rotate);
if ($entity) {
$entities[] = $entity;
Expand All @@ -473,6 +478,9 @@ private function readEntitiesSection($values, $addEntities = false, $move = [0,0
$data['knots'] = [];
$data['points'] = [];
}
if ($value['value'] == 'LWPOLYLINE') {
$data['points'] = [];
}
} else {
if ($entityType == 'SPLINE' && in_array($value['key'], [10, 20, 30, 40])) {
switch ($value['key']) {
Expand All @@ -487,6 +495,16 @@ private function readEntitiesSection($values, $addEntities = false, $move = [0,0
$data['knots'][] = $value['value'];
break;
}
} elseif ($entityType == 'LWPOLYLINE' && in_array($value['key'], [10, 20, 42])) {
switch ($value['key']) {
case 10:
$data['points'][] = [10 => $value['value'], 20 => 0, 42 => 0];
break;
case 20:
case 42:
$data['points'][sizeof($data['points']) -1 ][$value['key']] = $value['value'];
break;
}
} elseif (in_array($entityType, $types) || $entityType == 'POLYLINE') {
$data[$value['key']] = $value['value'];
} elseif ($entityType == 'VERTEX') {
Expand Down Expand Up @@ -599,10 +617,14 @@ private function addReadEntity($type, $data, $move = [0,0,0], $rotate = 0) {
$insert = new Insert($data[2], $point, $scale, $rotation);
$insert->move($move);
return $insert;
case 'LWPOLYLINE':
case 'POLYLINE':
case 'VERTEX':
if (isset($data[100])) {
switch ($data[100]) {
case 'AcDbPolyline':
$polyline = new LWPolyline();
break;
case 'AcDb2dPolyline':
$polyline = new Polyline(2);
break;
Expand Down
32 changes: 30 additions & 2 deletions lib/LWPolyline.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
class LWPolyline extends Entity {
protected $points = array();
protected $bulges = array();
protected $dimension;

/**
Expand All @@ -33,15 +34,41 @@ function __construct($dimension = 2) {
* Add a point to the LWPolyline
* @param array $point
*/
public function addPoint($point) {
public function addPoint($point, $bulge = 0) {
$this->points[] = $point;
$this->bulges[] = $bulge;
return $this;
}

public function getPoints() {
return $this->points;
}

public function getBulges() {
return $this->bulges;
}

/**
* Public function to move a Polyline entity
* @param array $move vector to move the entity with
*/
public function move($move) {
foreach ($this->points as &$point) {
$this->movePoint($point, $move);
}
}

/**
* Public function to rotate all points of a polyline
* @param int $rotate degree value used for the rotation
* @param array $rotationCenter center point of the rotation
*/
public function rotate($rotate, $rotationCenter = array(0, 0, 0)) {
foreach ($this->points as &$point) {
$this->rotatePoint($point, $rotationCenter, deg2rad($rotate));
}
}

/**
* Public function to render an entity, returns a string representation of
* the entity.
Expand All @@ -53,8 +80,9 @@ public function render() {
array_push($output, 90, count($this->points));
array_push($output, 70, $this->flagsToString());

foreach ($this->points as $point) {
foreach ($this->points as $i => $point) {
array_push($output, $this->point($point));
array_push($output, 42, $this->bulges[$i]);
}

return implode(PHP_EOL, $output);
Expand Down

0 comments on commit 183c307

Please sign in to comment.